Page 1 of 1

Nested Lambda Expressions and Such...

Posted: Wed May 14, 2014 4:37 pm
by macrolyte
Greets.

I'm having a problem understanding nested lambda expressions. In particular, the example I've banged my head on:

Code: Select all

(λ(a b)(λ(m)(m a b)))  ;; Where m is a function
So ...

Code: Select all

((λ(a b)(λ(m)(funcall m a b))#'*) 3 5) ;; returns => #<SYSTEM-FUNCTION *>in clisp/emacs/slime
The only thing I was able to find after searching the web was this, which, I *think*, suggests that I turn the statement into nested lambda functions of one variable.

As an aside, in some of my previous posts, I noted that some of the replies have taken the problem set and converted the solution as nested lambda expressions.

As this is a skill set I feel is really necessary, could someone tell me what book or resource I need to buy, 'cause the google really ain't helping. Thanks.

Re: Nested Lambda Expressions and Such...

Posted: Wed May 14, 2014 6:13 pm
by sylwester
So a lambda expression is just the source expression of a function..

Code: Select all

  ((lambda (x y) (+ x y)) 5 2) ; ==> 7
When evaluated they are functions and functions can be passed in parameters and returned.
A lambda expression which evaluates to the result of a lambda expression returns a function. You may choose to store it in one of the namespaces or call it.

Code: Select all

  ;; variable namespace
  (setf plus5 ((lambda (x) (lambda (y) (+ x y))) 5))
  (funcall plus5 2) ; ==> 7
  plus5                 ; ==> #<function plus5 ...>

  ;; anonymous
  (funcall ((lambda (x) (lambda (y) (+ x y))) 5) 2) ; ==> 7

  ;; funtion namespace
  (setf (symbol-function 'plus5) plus5)
  (plus5 2) ; ==> 7
  #'plus5    ; ==> #<function plus5 ...>
If you would have nested 3 lambdas then the second call would produce another function object which you then could call. It's not so difficult to follow the second you understand that any function has this property. Eg.

Code: Select all

  (setf plus #'+) ; #'<xx> retrieves the fucntion <xx> from the function makespace
  (funcall plus 5 2) ; ==> 7
  (funcall #'+ 5 2)   ; ==> 7

Re: Nested Lambda Expressions and Such...

Posted: Thu May 15, 2014 6:30 pm
by macrolyte
Thanks for the reply... I learned most of what you posted when I started learning about closures, but hey, since I'm still a n00b, better sage than sorry. I had to look at the situation for myself and realized that I had to approach the problem in pieces. On the webpage I posted above:

Code: Select all


(defun smaller (x y) 
      (if (< x y) x y))                   ;; is equivalent to =>

(lambda (x)(lambda (y) (if (< x y) x y))) ;; which yields =>

(funcall (funcall(function (lambda (x) (function (lambda (y) (if (< x y) x y)))) )4)9)  ;; returns =>4

Not life threatening, so I tried this:

Code: Select all


(funcall (funcall #'(lambda(x)#'(lambda(y)(if (< x y) x y)))4)9) ;; different syntax, returns =>4

(funcall (funcall #'(lambda(p)#'(lambda(q)(* (funcall q 5) p)))8)#'sqrt);; note position of variables during application. returns => 17.888544


Which makes more sense to me. However I'm still having trouble with the original problem:

Code: Select all


(defun d(a b)
    (* a b)) ;; see below

(funcall (funcall #'(lambda(a)#'(lambda(b)#'(lambda(m)(funcall m a b)))#'d)8)9)  ;; note the change in position for variable application. 

returns => too few arguments to D

I couldn't see what was going on in the stepper with just #'*, so I had to introduce #'d . So if you could help with this, it would really help. Thanks again.

m.

Re: Nested Lambda Expressions and Such...

Posted: Thu May 15, 2014 9:57 pm
by edgar-rft
Try this one:

Code: Select all

(funcall (funcall (funcall (lambda (a)
                             (lambda (b)
                               (lambda (m)
                                 (funcall m a b))))
                           8) 9) #'*)
You can't funcall m as long as a and b are not established, otherwise you'll get a "bad arguments" or similar error.

If you want to specify #'* first then you need to change the lambda nesting:

Code: Select all

(funcall (funcall (funcall (lambda (m)
                             (lambda (a)
                               (lambda (b)
                                 (funcall m a b))))
                           #'*) 8) 9)

Re: Nested Lambda Expressions and Such...

Posted: Fri May 16, 2014 8:44 am
by macrolyte
VIELEN DANK!. I had noticed in the stepper that there appeared to be only (2) applications were occurring, (for the 'b and 'm lambda functions). I was thinking of adding another funcall, then I saw your reply. I haven't been well of late, so please bear with my shortcomings for now. Again, thanks.