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

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

Re: About function parameters, is it a bug of elisp interpreter?


From: Trent Buck
Subject: Re: About function parameters, is it a bug of elisp interpreter?
Date: Tue, 22 May 2007 12:47:01 +1000
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (gnu/linux)

"Lee David" <live4thee@gmail.com> writes:

> When porting some Scheme functions to Elisp, I encountered a strange
> (to me) problem as listed below.
>
> (defun sum (term beg next b)
>  (defun iter (beg result)
>    (if (> beg b)
>       result
>      (iter (funcall next beg) (+ result (funcall term beg)))))
>
>  (iter beg 0))

Elisp does not have Scheme-style block structure.  DEFUN always
changes the global procedure definition.  This can be demonstrated
trivially:

    ;; in Emacs
    ELISP> (defun f ()
             (defun g ()
               2)
             (g))
    f
    ELISP> (f)
    2
    ELISP> (fboundp 'g)
    t

    ;; in Scheme
    #;1> (define (f)
           (define (g)
             2)
           (g))
    #;2> (f)
    2
    #;3> g
    Error: unbound variable: g

Similarly, R5RS Scheme always uses lexical scoping, whereas elisp is
dynamically scoped (by default).

    ;; in Emacs
    ELISP> (defvar x 2)
    x
    ELISP> (defun f ()
             x)
    f
    ELISP> (let ((x 3))
             (f))
    3

    ;; in Scheme
    #;1> (define x 2)
    #;2> (define (f)
           x)
    #;3> (let ((x 3))
           (f))
    2

These are not so much bugs as simply different semantics.  You will
need to understand them in order to correctly translate code between
Scheme and elisp.
-- 
Trent Buck





reply via email to

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