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

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

Re: simple... bug?


From: Joakim Hove
Subject: Re: simple... bug?
Date: Tue, 14 Oct 2003 11:12:46 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.2 (gnu/linux)

Joe Corneli <jcorneli@math.utexas.edu> writes:

> (defun squares (list-of-numbers)
>   (let ((list-of-squares nil)
>   (i 0)
>   (L (length list-of-numbers)))
>     (while (< i L)
>       (setq list-of-squares (append list-of-squares 
>                                       (list (* (nth i list-of-numbers)
>                                                            (nth i
>   list-of-numbers)))))
>       (setq i (1+ i))))
>   list-of-squares)

Hello,

as already pointed out by Alan the "list-of-squares" variable is out
of scope when you return it, so I *guess* it is only accidental that
it is so seemingly correct. Moving a closing parenthesis to include
the return value  within the let block should solve your problem:

  list-of-numbers)))))
      (setq i (1+ i)))
  list-of-squares ) )
                  ^ 
                 / \
                Matches the (let  )



Anyway, a simpler solution:

(defun squares (nr-list)
  (mapcar '(lambda (x) (* x x)) nr-list))

Use C-h f mapcar to learn about the function mapcar.


HTH - Joakim



-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/


reply via email to

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