all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Lee David" <live4thee@gmail.com>
To: bug-gnu-emacs@gnu.org
Subject: About function parameters, is it a bug of elisp interpreter?
Date: Tue, 22 May 2007 02:22:56 +0800	[thread overview]
Message-ID: <8f62e1cd0705211122p2b920a9ag8092de66c0c0751a@mail.gmail.com> (raw)

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	other threads:[~2007-05-21 18:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-21 18:22 Lee David [this message]
2007-05-22  2:47 ` About function parameters, is it a bug of elisp interpreter? Trent Buck
2007-05-22  6:03   ` Lee David

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8f62e1cd0705211122p2b920a9ag8092de66c0c0751a@mail.gmail.com \
    --to=live4thee@gmail.com \
    --cc=bug-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.