This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Nested Lambda Expressions and Such...

5 posts · 5800 views

Greets.

I'm having a problem understanding nested lambda expressions. In particular, the example I've banged my head on:
(λ(a b)(λ(m)(m a b)))  ;; Where m is a function
So ...
((λ(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...

So a lambda expression is just the source expression of a function..
  ((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.
  ;; 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.
  (setf plus #'+) ; #'<xx> retrieves the fucntion <xx> from the function makespace
  (funcall plus 5 2) ; ==> 7
  (funcall #'+ 5 2)   ; ==> 7
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Re: Nested Lambda Expressions and Such...

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:
(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:
(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:
(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.

Last edited by macrolyte on , edited 1 time in total.

Re: Nested Lambda Expressions and Such...

Try this one:
(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:
(funcall (funcall (funcall (lambda (m)
                             (lambda (a)
                               (lambda (b)
                                 (funcall m a b))))
                           #'*) 8) 9)

Re: Nested Lambda Expressions and Such...

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.