help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scheme to elisp


From: Joost Kremers
Subject: Re: Scheme to elisp
Date: 16 May 2007 15:58:07 GMT
User-agent: slrn/0.9.8.1 (Linux)

weber wrote:
> Or in a simplified form, my problem is : why the snippet below does
> not work?

[note: the original indentation was confusing:]

> (defun f (n)
>   (let ((g (lambda (x) (+ 5 x))))
>     (g (+ n 1))))
>
> (f 1) -> void-function g

well, elisp is a lisp2, rather than a lisp1 (as scheme is), so a symbol can
have both a value as a variable *and* a function definition. with let, you
assign g a value *as a variable*, it does not affect the *function*
definition of the symbol g.

it is possible to assign an anonymous function as the variable value of a
symbol. if you do this, you can use either funcall or apply (they have
different uses) to call the (anonymous) function stored in the variable,
like this:

(defun f (n)
  (let ((g (lambda (x) (+ 5 x))))
    (funcall g (+ n 1))))

alternatively, if you use the common-lisp compatibility package, you can
use either flet or labels to create function bindings:

(defun f (n)
  (flet ((g (x) (+ 5 x)))
    (g (+ n 1))))
       
note that with flet, you don't use anonymous functions. the syntax is:

(flet ((<function_name> <arg_list> . <function_body>))
  (<flet_body>)
  ...)    

HTH


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]