Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lambda inside a let or letrec

148 views
Skip to first unread message

bolega

unread,
Jun 10, 2010, 1:34:14 AM6/10/10
to
My apologies in advance to comp.lang.scheme and comp.lang.lisp.

I am trying to run a certain syntax inside emacs lisp.

I know basically how let works

(let (list of pairs of var value) (function))

This is like a lambda function call , only the order is different.

But the novelty i saw reading a book on common lisp or scheme is this
and i failed to run in emacs. plz tell what modifications are needed
and i know they are different.

( (lambda (n) (+ 1 n)) 3) ;;; works in emacs

(let
((a (lambda (n) (+ 1 n))) (b 3)) (a b)) ;;; does NOT work in emacs

basically we are trying to use / abuse the let in that in the pair we
define a equal to a lambda. Then another pair where a value of b is
defined.

next, we want a to operate on b.

Why does it fail ?

The scheme/lisp book/paper where it was seen (forgot) used letrec.

Can someone enlighten me how set! and let can be used to formulate
recursion when the let has no recursion built in it ?

thanks a lot.
cheers


bolega

unread,
Jun 10, 2010, 1:37:38 AM6/10/10
to
let me write it more clearly with indents as follows :

(let
((a (lambda (n) (+ 1 n)))
(b 3))
(a b))

bolega

unread,
Jun 10, 2010, 1:37:49 AM6/10/10
to
let me write it more clearly with indents as follows :

(let


((a (lambda (n) (+ 1 n)))
(b 3))
(a b))

bolega

unread,
Jun 10, 2010, 2:18:25 AM6/10/10
to

The following works where I have replaced the association of lambda to
a _by_ the association of lambda function call to a, ie a numerical
value.

(let
((a ((lambda (n) (+ 1 n)) 5))
(b 3))
(+ a b))

I am sure I am making some trivial mistake

David Kastrup

unread,
Jun 10, 2010, 3:22:37 AM6/10/10
to
bolega <gnui...@gmail.com> writes:

> let me write it more clearly with indents as follows :
>
> (let
> ((a (lambda (n) (+ 1 n)))
> (b 3))
> (a b))

Scheme does not have separate value and function cells for symbols.
Lisp does. let binds the value cell of a function. To call a function
in the value cell of a symbol, use funcall. To bind a lambda formto the
function cell of a symbol, use flet (from the cl library).

So you can do either

(let
((a (lambda (n) (+ 1 n)))
(b 3))

(funcall a b))

or

(eval-when-compile (require 'cl))
(flet ((a (n) (+ 1 n)))
(let ((b 3))
(a b)))

--
David Kastrup

Tim Bradshaw

unread,
Jun 10, 2010, 4:46:40 AM6/10/10
to
On 2010-06-10 08:22:37 +0100, David Kastrup said:

> Scheme does not have separate value and function cells for symbols.
> Lisp does.

To be precise: some Lisps do, and Common Lisp and Emacs Lisp are among
those. Some don't (for instance Cambridge Lisp didn't, Standard Lisp
may not have but I can't remember).

I guess what I'm trying to say is that this isn't a defining difference
between Lisp(s) and Scheme(s), if one wanted to make such a distinction.

--tim

Pascal J. Bourguignon

unread,
Jun 10, 2010, 10:28:02 AM6/10/10
to
bolega <gnui...@gmail.com> writes:

As indicated by David, it's because scheme (and eg. LeLisp) is a
Lisp-1 while emacs lisp and Common Lisp are Lisp-2.

In a Lisp-1 there's a single slot for both values and functions,
functions are just values like others, and a symbol has only one
value, which may be a function or something else.

In this case, both LET and the function application refer the same
unique value of the symbol.

This has the advantage of being simple conceptually, and to be able to
write with a simplier syntax function code of higher order.


In a Lisp-2 there are two slots, one for a value (which may be a
function too) and one for a function (which may only be unbound or a
function).

In this case, depending on the context, the value slot XOR the
function slot is used. LET binds the value slot, the symbol by itself
evaluates to the contents of its value slots (when it's a special
variable). On the other hand DEFUN and function application refer the
function slot. This has the advantage of reducing a lot of collision
problems in writing unhygienic macros, and to offer a double meaning
that matches more closely natural languages. In a Lisp-2 you can write:

(let ((buffalo (get-a-bull)))
(flet ((buffalo (what) (do-it what)))
(buffalo buffalo)))

and have (do-it (get-a-bull)) executed.

On the other hand, if you want to use a value as a function, you have
to go thru APPLY or FUNCALL:

(let ((fun (lambda (x) (be-happy-with x))))
(funcall fun 'foot))


In a Lisp-1 you would have to write:

(let ((buffalo-as-noun (get-a-bull))
(buffalo-as-verb (lambda (what) (do-it what))))
(buffalo-as-verb buffalo-as-noon))


Now, IIRC, Pascal Costanza has published in cll a few years ago a
macro that will allow you to write Lisp-1 function applications in a
Lisp-2, so that when you need locally to call a lot of functions
stored in value slots you may write it like in scheme.
Try to groups.google cll for Pascal Costanza Lisp-1 macro.


--
__Pascal Bourguignon__
http://www.informatimago.com

Thomas A. Russ

unread,
Jun 10, 2010, 1:54:48 PM6/10/10
to
bolega <gnui...@gmail.com> writes:

> let me write it more clearly with indents as follows :
>
> (let
> ((a (lambda (n) (+ 1 n)))
> (b 3))
> (a b))

This will work in a so-called 'lisp-1' like Scheme.
It will not work in a so-called 'lisp-2' like Common Lisp or Emacs Lisp.

The issue has to do with whether or not there is a separate function
value namespace or not. In Common Lisp or Emacs Lisp you need to use
FUNCALL to make it work

(let ((a (lambda (n) (+ 1 n)))
(b 3))

(funcall a b))

because the LAMBDA expression you want is in the value namespace (slot)
of the symbol A and not in the function namespace (slot).

--
Thomas A. Russ, USC/Information Sciences Institute

bolega

unread,
Jun 10, 2010, 8:42:10 PM6/10/10
to
thank you so much to all who replied. I gave max star rating in google
to all replies so others can find it easily
0 new messages