lambda functions ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maxim Veksler

    #1

    lambda functions ?

    Hello,
    I'm new on this list and in python.

    It seems python has some interesting concept of "ad hoc" function
    which I'm trying to understand without much success.

    Take the following code for example:

    """
    >>def make_incremento r(n):
    .... return lambda x: x + n
    ....
    >>f = make_incremento r(42)
    >>f(0)
    42
    >>f(1)
    43
    """

    I really don't understand whats going on here.
    On the first instantiating of the object "f" where does "x" gets it's
    value? Or is it evaluated as 0? ie "x: 0 + 42"

    And what is the "f" object? An integer? a pointer? an Object?
    I'm coming from the C world...

    Could some please try (if even possible) to implement the above code
    without using "lambda" I believe it would help me grasp this a bit
    faster then.

    Thank you,
    Maxim.


    --
    Cheers,
    Maxim Veksler

    "Free as in Freedom" - Do u GNU ?
  • Paul Rubin

    #2
    Re: lambda functions ?

    "Maxim Veksler" <hq4ever@gmail. comwrites:
    >def make_incremento r(n):
    ... return lambda x: x + n
    Is the same as:

    def make_incremento r(n):
    def inner(x):
    return x + n
    return inner

    When you enter make_incremento r, it allocates a memory slot (normally
    we'd think of this as a stack slot since it's a function argument, but
    it's really in the heap) and copies its argument there. Then you
    create the function "inner" which refers to that memory slot because
    of the reference to "n". Then make_incremento r returns, but since the
    returned object still contains the reference to that memory slot, the
    memory slot is not freed (this is the part where it becomes important
    that the slot is really not on the stack). So when you call the
    returned function, it still can get at that slot.

    This style is very common in Scheme programming so you might read a
    Scheme book if you want to understand it. The classic:

    http://mitpress.mit.edu/sicp/
    Could some please try (if even possible) to implement the above code
    without using "lambda" I believe it would help me grasp this a bit
    faster then.
    Does the above help?

    Comment

    • Bruno Desthuilliers

      #3
      Re: lambda functions ?

      Maxim Veksler a écrit :
      Hello,
      I'm new on this list and in python.
      Welcome on board...
      It seems python has some interesting concept of "ad hoc" function
      which I'm trying to understand without much success.
      >
      Take the following code for example:
      >
      """
      >
      >>>def make_incremento r(n):
      >
      ... return lambda x: x + n
      ...
      >
      >>>f = make_incremento r(42)
      >>>f(0)
      >
      42
      >
      >>>f(1)
      >
      43
      """
      >
      I really don't understand whats going on here.
      On the first instantiating of the object "f" where does "x" gets it's
      value?
      Nowhere. The above code is strictly equivalent to:

      def make_incremento r(n):
      def inc(x):
      return x + n
      return inc

      It's based on the fact that (for short):
      1/ Python functions are first-class objects, and as such can be passed
      around just like any other object
      2/ Python functions are closures, meaning they carry the environment in
      which they where defined.

      IOW, make_incremento r(n) creates and returns a new function each time
      it's called. This function remembers the value of n with which it was
      created, and just awaits to be called with the missing x arg.

      Or is it evaluated as 0? ie "x: 0 + 42"
      >
      And what is the "f" object? An integer? a pointer? an Object?
      an object, instance of class function.
      I'm coming from the C world...
      >
      Could some please try (if even possible) to implement the above code
      without using "lambda" I believe it would help me grasp this a bit
      faster then.
      cf above.

      FWIW, lambda is just a syntactic sugar for creating anonymous,
      dead-simple functions, usually used as callbacks for generic functions
      or methods like map, filter, list.sort etc.

      FWIW(2), this programming style (anonymous functions, closures etc)
      comes from functional programming (Haskell, ML, lisp, etc...).
      Thank you,
      HTH

      Comment

      • Toby A Inkster

        #4
        Re: lambda functions ?

        Maxim Veksler wrote:
        And what is the "f" object? An integer? a pointer? an Object?
        A function.

        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~ http://tobyinkster.co.uk/contact
        Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

        * = I'm getting there!

        Comment

        • Maxim Veksler

          #5
          Re: lambda functions ?

          Wow,

          Thank you everyone for the help. I am amazed by the motivation people
          have on this list to help new comers. I hope that I will be able to
          contribute equally some day.

          On 05 Feb 2007 14:22:05 -0800, Paul Rubin
          <"http://phr.cx"@nospam. invalidwrote:
          "Maxim Veksler" <hq4ever@gmail. comwrites:
          >>def make_incremento r(n):
          ... return lambda x: x + n
          >
          Is the same as:
          >
          def make_incremento r(n):
          def inner(x):
          return x + n
          return inner
          >
          When you enter make_incremento r, it allocates a memory slot (normally
          we'd think of this as a stack slot since it's a function argument, but
          it's really in the heap) and copies its argument there. Then you
          create the function "inner" which refers to that memory slot because
          of the reference to "n". Then make_incremento r returns, but since the
          returned object still contains the reference to that memory slot, the
          memory slot is not freed (this is the part where it becomes important
          that the slot is really not on the stack). So when you call the
          returned function, it still can get at that slot.
          >
          Following the debugger on your code, I can identify the following stages:

          def make_incremento r(n):
          def inner(x):
          return x + n
          return inner

          f = make_incremento r(10)
          f(10)
          f(0)

          1. "def make_incremento r(n)" Create function object in memory and set
          "make_increment or" to point to it.
          2. "f = make_incremento r(10)" Set "f" to point to "inner(x) function".
          Set n value to 10.
          3. "f(10)" Call to inner(x), which will return "father" n + "local" x.

          This means that "f" is not a pointer to make_incremento r but rather to
          the internal (copied?) function.
          This style is very common in Scheme programming so you might read a
          Scheme book if you want to understand it. The classic:
          >
          http://mitpress.mit.edu/sicp/
          >
          I might just well do that.
          Could some please try (if even possible) to implement the above code
          without using "lambda" I believe it would help me grasp this a bit
          faster then.
          >
          Does the above help?
          all
          Your explanation was excellent. Thank you.

          --
          Cheers,
          Maxim Veksler

          "Free as in Freedom" - Do u GNU ?

          Comment

          • Eduardo \EdCrypt\ O. Padoan

            #6
            Re: lambda functions ?

            This means that "f" is not a pointer to make_incremento r but rather to
            the internal (copied?) function.
            "returned" function isthe right here. As any returned object from a function.
            >
            This style is very common in Scheme programming so you might read a
            Scheme book if you want to understand it. The classic:

            http://mitpress.mit.edu/sicp/
            >
            I might just well do that.
            A nice read indeed, but understand this concept first:


            --
            EduardoOPadoan (eopadoan->altavix::com )
            Bookmarks: http://del.icio.us/edcrypt
            Blog: http://edcrypt.blogspot.com
            Jabber: edcrypt at jabber dot org
            ICQ: 161480283
            GTalk: eduardo dot padoan at gmail dot com
            MSN: eopadoan at altavix dot com

            Comment

            Working...