Nested Lambda Expressions and Such...

Discussion of Common Lisp

Nested Lambda Expressions and Such...

Postby macrolyte » Wed May 14, 2014 4:37 pm

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.
macrolyte
 
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Nested Lambda Expressions and Such...

Postby sylwester » Wed May 14, 2014 6:13 pm

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
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm

Re: Nested Lambda Expressions and Such...

Postby macrolyte » Thu May 15, 2014 6:30 pm

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.
Last edited by macrolyte on Fri May 16, 2014 10:32 am, edited 1 time in total.
macrolyte
 
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Nested Lambda Expressions and Such...

Postby edgar-rft » Thu May 15, 2014 9:57 pm

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)
edgar-rft
 
Posts: 157
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Nested Lambda Expressions and Such...

Postby macrolyte » Fri May 16, 2014 8:44 am

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.
macrolyte
 
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 4 guests