[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: lambda inside a let or letrec
From: |
Helmut Eller |
Subject: |
Re: lambda inside a let or letrec |
Date: |
Wed, 08 Dec 2010 15:12:53 -0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) |
* bolega [2010-06-10 07:34+0200] writes:
> 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 first element of a call like (FUN ARG0 ARG1 ...), that is FUN, is
special. FUN must be one of:
1) be the name of function (like "+")
2) the name of a macro (like "save-excursion")
3) the name of a special form (like "let", or "while")
4) a lambda form (e.g ((lambda (x) (+ x x) 1)))
So if you write the (a b) the evaluation rules for a are different as
the rules for b. In your example "a" is a variable but not a function.
In techical terms one would say that a is bound in the variable
namespace but not in the function namespace.
The simplest solution to call a variable is: (funcall a b)
> The scheme/lisp book/paper where it was seen (forgot) used letrec.
It was probably a Scheme book, because the rules are different in this
regard in Scheme. In Scheme a call (FUN ARG0 ...) is essentially
the same as (funcall FUN ARG0 ...) in Lisp
> Can someone enlighten me how set! and let can be used to formulate
> recursion when the let has no recursion built in it ?
Well, in Emacs Lisp it's easy:
(let ((fac (lambda (x) (if (zerop x) 1 (* x (funcall fac (1- x)))))))
(funcall fac 10))
that works because Emacs uses dynamic scoping, so you don't need setq.
Helmut