From: Sonya Allin
Subject: recursive lambdas?
Date: 
Message-ID: <Pine.GSO.3.95qL.981113002759.28512A-100000@sawasdee.cc.columbia.edu>
hi -- i am trying to write a function right now that will return a
function.  the function that it is to return is to combine a symbol with
the various elements in a list.  like this:

(defun make-function-combine (a-symbol) 
	#'(lambda (x) (something goes in here what it is i dont know))

anyway the idea is to call this function like so

(setf (symbol-function 'something) (make-function-combine 'element))

so that i amy then call

(something '(1 2 3) )

and have it return

((1 element) (2 element) (3 element))

so.  

i have tried

(defun make-function-combine (a-symbol) 
	#'(lambda (x) (append (list (first x) a-symbol)
	(make-function-combine (rest x)))

which is pretty obviously wrong but illustrates what i want to do -- i
want to create a lambda that is capable of calling itself.

does anyone out there know how to do this sort of thing?  i would
appreciate advice mucho mucho...

From: Espen Vestre
Subject: Re: recursive lambdas?
Date: 
Message-ID: <w6n25w59ly.fsf@gromit.nextel.no>
Sonya Allin <····@columbia.edu> writes:

> which is pretty obviously wrong but illustrates what i want to do -- i
> want to create a lambda that is capable of calling itself.

You can't easily define recursive lambdas (you must e.g. use "labels").  
But you don't really want to define a recursive function here.  
Since this smells like homework (my apologies if it isn't!), I'll 
just give you a hint: mapcar!

--

  best wishes,
    Espen Vestre
From: Rainer Joswig
Subject: Re: recursive lambdas?
Date: 
Message-ID: <joswig-1311980926040001@194.163.195.67>
In article
<··········································@sawasdee.cc.columbia.edu>,
Sonya Allin <····@columbia.edu> wrote:

> hi -- i am trying to write a function right now that will return a
> function.  the function that it is to return is to combine a symbol with
> the various elements in a list.  like this:
> 
> (defun make-function-combine (a-symbol) 
>         #'(lambda (x) (something goes in here what it is i dont know))
> 
> anyway the idea is to call this function like so
> 
> (setf (symbol-function 'something) (make-function-combine 'element))
> 
> so that i amy then call
> 
> (something '(1 2 3) )
> 
> and have it return
> 
> ((1 element) (2 element) (3 element))
> 
> so.  
> 
> i have tried
> 
> (defun make-function-combine (a-symbol) 
>         #'(lambda (x) (append (list (first x) a-symbol)
>         (make-function-combine (rest x)))
> 
> which is pretty obviously wrong but illustrates what i want to do -- i
> want to create a lambda that is capable of calling itself.
> 
> does anyone out there know how to do this sort of thing?  i would
> appreciate advice mucho mucho...

I appreciate that you have written more like "please solve
my homework assignment". Here are
two versions:

(defun make-function-combine (a-symbol) 
  #'(lambda (the-list)
      (when the-list
        (cons (list (first the-list)
                    a-symbol)
              (funcall (make-function-combine a-symbol)
                       (rest the-list))))))


(defun make-function-combine (a-symbol)
  (labels ((combine (the-list)
             (when the-list
               (cons (list (first the-list)
                           a-symbol)
                     (combine (rest the-list))))))
    #'combine))

Now you have to explain these... ;-)

-- 
http://www.lavielle.com/~joswig
From: Tim Bradshaw
Subject: Re: recursive lambdas?
Date: 
Message-ID: <ey31zn75387.fsf@todday.aiai.ed.ac.uk>
* Sonya Allin wrote:
> hi -- i am trying to write a function right now that will return a
> function.  the function that it is to return is to combine a symbol with
> the various elements in a list.  like this:

> (defun make-function-combine (a-symbol) 
> 	#'(lambda (x) (something goes in here what it is i dont know))

> anyway the idea is to call this function like so

> (setf (symbol-function 'something) (make-function-combine 'element))

> so that i amy then call

> (something '(1 2 3) )

> and have it return

> ((1 element) (2 element) (3 element))

You will get full marks, I'm sure, if you can explain this:

    (defun make-function-combine (s)
      ((lambda (f)
	 #'(lambda (l)
	     (funcall f l '() f)))
       #'(lambda (l a c)
	   (if (null l)
	       ((lambda (c)
		  (funcall c  a '() c))
		#'(lambda (r a c)
		    (if (null r)
			a
			(funcall c (cdr r) (cons (car r) a) c))))
	       (funcall c (cdr l)
			(cons (cons (car l) s) a) c)))))

--tim
From: Michael Tuchman
Subject: Re: recursive lambdas? (warning: contains some flame bait - bring salt)
Date: 
Message-ID: <wklnlbpj82.fsf_-_@orsi.com>
** on soapbox **
Guys, let's lighten up on this one.  I found out that this was a
research question from someone trying to learn lisp independently for
a research project.  Not all beginners questions are homework.  We
need to find a way to be friendlier to self study questions.

This particular query showed enough effort on the posters part to
warrant a friendlier response, IMO.  
** off soapbox**

-- 
Michael Tuchman
A Great way to avoid flame wars: all.SCORE has  ("vs\\.?" -1000 nil r) 
From: Gareth McCaughan
Subject: Re: recursive lambdas? (warning: contains some flame bait - bring salt)
Date: 
Message-ID: <867lwuwoua.fsf@g.pet.cam.ac.uk>
Michael Tuchman wrote:

> ** on soapbox **
> Guys, let's lighten up on this one.
[etc]

You're probably right, but the bizarre solutions to beginners'
problems are one of the most enjoyable features of c.l.l for me.
It's the same sort of appeal as the Obfuscated C Code Competitions.
(No, obviously I'm not proposing that we should flame people just
in order to give a few cheap laughs.)

-- 
Gareth McCaughan       Dept. of Pure Mathematics & Mathematical Statistics,
·····@dpmms.cam.ac.uk  Cambridge University, England.
From: Michael Tuchman
Subject: Re: recursive lambdas?
Date: 
Message-ID: <wkd86rbl5u.fsf@orsi.com>
I am simply going to take you on your honor that this is not a
homework problem.  Since you gave us your own attempt that (IMO)
entitles you to some hints and discussion:

What's wrong with (aside from efficency & garbage issues) this?

(defun combine (a-symbol)
  #'(lambda (x) (mapcar #'(lambda (m) (list a-symbol m)) x))) 

Of course your problem may actually be more general than what you
wrote, and you were just giving us a simplified example.

If recursive lambdas are what you really want, you may wish to take a
look at the alambda macro in Paul Graham's OnLISP.  I think he has the
code for his book on-line. ftp://ftp.das.harvard.edu/pub/onlisp/onlisp.lisp

However I do wonder if recursive lambda are what you really need here.

; This code is copyright 1993 by Paul Graham, but anyone who wants 
; to use the code in any nonprofit activity, or distribute free
; verbatim copies (including this notice), is encouraged to do so.

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

Now you can say
#'(alambda (x) 
     (if (= x 0)
         1
       (* x (self (1- x)))))

to specify the factorial function

My point of view is that these small recursive functions are often
created just to be called by one other function, in which case a
labels statement may be appropriate, as in:

(defun something (l a-symbol)
   (labels ((make-function-combine (ll)
	      (if (null ll) nil
                 (cons (list a-symbol (first ll))
		       (make-function-combine (rest ll))))))
       (make-function-combine l)))


Coming back to your attempt:

(defun make-function-combine (a-symbol)
  #'(lambda (x)
      ;; do you mean cons here?
      ;; there is no test for (null x). I presume you left
      ;; this out for brevity
      (append  (list (first x) a-symbol)
	 (make-function-combine (rest x)))))

Take a look at this skeleton and compare it to your code
(defun f (x)
   (if (null x) nil
     (cons (??? (first x) (f (rest x))
  
If you'll notice, this really is just the definition of mapcar (more
or less). That brings me back to the function at the beginning of this
post, so perhaps a good time to stop.

Hope that helped.

----------
Here's a transcript of what I wrote in action
CL-USER 1 > (defun combine (a-symbol)
  #'(lambda (x) (mapcar #'(lambda (m) (list a-symbol m)) x)))
COMBINE

CL-USER 2 > (setf (symbol-function 'sonya) (combine 'something))
#<closure SPECIAL::APPLY-INTERPRETED-CLOSURE 20F9CFFA>

CL-USER 3 > (sonya (list 1 2 3))
((SOMETHING 1) (SOMETHING 2) (SOMETHING 3))

;; well I got it backwards, but you get the idea.