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

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

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


From: Lee David
Subject: About function parameters, is it a bug of elisp interpreter?
Date: Tue, 22 May 2007 02:22:56 +0800

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




reply via email to

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