From: Barry Margolin
Subject: Re: constructing a lambda
Date: 
Message-ID: <barmar-88EC7D.20263728012006@comcast.dca.giganews.com>
In article <·····················@ram.dialup.fu-berlin.de>,
 ···@zedat.fu-berlin.de (Stefan Ram) wrote:

>   I tried to solve the word puzzle by Frank Buss, took my first
>   steps into Common Lisp, and immediatly encountered a problen:
> 
> ( defun alpha () "alpha" )
> 
> ( defun beta ( x )( concatenate 'string x "beta" ))
> 
> ( defun bind( source sink )
>   ( lambda()( funcall sink( funcall source ))))
> 
> ( print ( bind #'alpha #'beta ))
> 
>   It prints:
> 
> #<FUNCTION :LAMBDA NIL (FUNCALL SINK (FUNCALL SOURCE))>
> 
>   What I'd like to see is:
> 
> #<FUNCTION :LAMBDA NIL "alphabeta")>
> 
>   I.e., the function �bind� should evaluate
> 
> ( funcall sink( funcall source ))
> 
>   to construct the lambda-body. This evaluation
>   will substitute the parameter bindings, resulting in
> 
> ( funcall #'beta ( funcall #'alpha ))
> 
>   which should give �alphabeta�, so that the lambda returned
>   by "bind" should become �( lambda() "alphabeta" )�.

First of all, please learn the proper way to format Lisp code.  Any Lisp 
textbook should show it.  Your spacing is really weird.

Anything you put inside a lambda expression will only be evaluated when 
the function it evaluates to is called, not at the time the lambda 
expression is being evaluaed.  So if you want something evaluated just 
once, you must do it outside the lambda expression:

(defun bind (source sink)
  (let ((result (funcall sink (funcall source))))
    (lambda () result)))

You still won't see "alphabeta" in the resulting function object, you'll 
see:

#<FUNCTION :LAMBDA NIL RESULT>

and the closure will have RESULT bound to "alphabeta".  If you really 
want the result as a literal in the lambda, you have to construct the 
expression on the fly:

(defun bind (source sink)
  (let ((result (funcall sink (funcall source))))
    (coerce `(lambda () ',result))))

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***