* About function parameters, is it a bug of elisp interpreter?
@ 2007-05-21 18:22 Lee David
2007-05-22 2:47 ` Trent Buck
0 siblings, 1 reply; 3+ messages in thread
From: Lee David @ 2007-05-21 18:22 UTC (permalink / raw)
To: bug-gnu-emacs
Hi, list
When porting some Scheme functions to Elisp, I encountered a strange
(to me) problem
as listed below.
;; compute intergral value of a math function with Simpson formula.
;; h/3 * [ Y0 + 4Y1 + 2Y2 + 4Y3 + 2Y4 + ... + 2Yn-2 + Yn ]
;; ---- [1] ----
;; (defun sum (term a next end)
;; (defun iter (a result)
;; (if (> a end)
;; result
;; (iter (funcall next a) (+ result (funcall term a)))))
;;
;; (iter a 0))
;; ---- [2] ----
(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))
;; ---- [3] ----
(defun simpson (f a b n)
(setq h (/ (- (float b) a) n))
(defun y (k)
(funcall f (+ a (* k h))))
(defun term (i)
(+ (y (- (* 2 i) 2))
(* 4 (y (- (* 2 i) 1)))
(y (* 2 i))))
(defun next (i)
(1+ i))
(/ (* h (sum 'term 1 'next (/ n 2))) 3))
(simpson (lambda (x) (* x x)) 0 1 100) ; 0.3333333333333333
--------------------------------------------- End Source
-------------------------------------------
The only difference between commented-out source code [1] and in-use code [2]
lies in that they chosed different parameter names, `a' for former and
`beg' for latter.
The problem is that [1] can't work when [3] choose the same name, i.e. `a'.
when both [1] and [3] choose the same name `a', I found that local function `y'
defined in `simpson' will get value `a' as 1, 2, 3, 4 ... 100 each
time it is called,
rather than constant zero as I assumed. I do not know why.
Is it a bug? Or, more probably, there are potential issues I didn't see here?
Hope someone can elaborate a little bit further, since I am newbie to elisp.
Not quite sure whether it is the right list to ask, if any better
choice, pls point out.
Tested on:
(version)
"GNU Emacs 23.0.0.1 (i386-mingw-nt5.1.2600)
of 2007-01-02 on DTOP"
and,
(version)
"GNU Emacs 21.4.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll bars)
of 2007-01-16 on palmer, modified by Debian"
--
Thanks,
Li Qun
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: About function parameters, is it a bug of elisp interpreter?
2007-05-21 18:22 About function parameters, is it a bug of elisp interpreter? Lee David
@ 2007-05-22 2:47 ` Trent Buck
2007-05-22 6:03 ` Lee David
0 siblings, 1 reply; 3+ messages in thread
From: Trent Buck @ 2007-05-22 2:47 UTC (permalink / raw)
To: bug-gnu-emacs
"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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: About function parameters, is it a bug of elisp interpreter?
2007-05-22 2:47 ` Trent Buck
@ 2007-05-22 6:03 ` Lee David
0 siblings, 0 replies; 3+ messages in thread
From: Lee David @ 2007-05-22 6:03 UTC (permalink / raw)
To: Trent Buck; +Cc: bug-gnu-emacs
On 5/22/07, Trent Buck <trentbuck@gmail.com> wrote:
> 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
Yes, I've already known this by trial.
> 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
Thanks for your explanation, and it should be the key to answer my confusion.
> 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.
A sensational subject may be apt to draw eyeballs. :)
Thanks very much.
> --
> Trent Buck
>
>
>
> _______________________________________________
> bug-gnu-emacs mailing list
> bug-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/bug-gnu-emacs
>
--
Thanks,
Li Qun
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-05-22 6:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-21 18:22 About function parameters, is it a bug of elisp interpreter? Lee David
2007-05-22 2:47 ` Trent Buck
2007-05-22 6:03 ` Lee David
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.