all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* simple... bug?
@ 2003-10-14  7:17 Joe Corneli
  0 siblings, 0 replies; 4+ messages in thread
From: Joe Corneli @ 2003-10-14  7:17 UTC (permalink / raw)


(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)

For

(squares '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
83 89 97))

I get

     *
(4 9 9 25 49 121 169 289 361 529 841 961 1369 1681 1849 2209 2809 3481
3721 4489 5041 5329 6241 6889 7921 9409)


Emacs is 21.3.1
OS is  Mac OS X 10.3


	      Am I making some _extremely_ subtle mistake?

--
Joe
-*-
The technological simulacrum creates its own reality,
which Baudrillard calls the "hyperreal," a kind of ersatz
parody of Plato's ideal world of forms.  fusionanomaly.net

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: simple... bug?
       [not found] <mailman.1649.1066115958.21628.help-gnu-emacs@gnu.org>
@ 2003-10-14  8:46 ` Alan Mackenzie
  2003-10-14  9:12 ` Joakim Hove
  2003-10-15  7:49 ` David Vanderschel
  2 siblings, 0 replies; 4+ messages in thread
From: Alan Mackenzie @ 2003-10-14  8:46 UTC (permalink / raw)


Joe Corneli <jcorneli@math.utexas.edu> wrote on Tue, 14 Oct 2003 02:17:11
-0500:

> (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)

> For

> (squares '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
> 83 89 97))

> I get

>      *
> (4 9 9 25 49 121 169 289 361 529 841 961 1369 1681 1849 2209 2809 3481
> 3721 4489 5041 5329 6241 6889 7921 9409)


> Emacs is 21.3.1
> OS is  Mac OS X 10.3

> 	      Am I making some _extremely_ subtle mistake?

What on earth is the value "list-of-squares" that you're returning?  It's
not the one you bound in the "let" form.  :-)

Hey, I do things like this too.  It makes me feel really stupid.
Something I've done approximately three times in the last three months is
to write this form:

(\= (point) here)

It didn't do what I wanted.  But thank goodness for edebug.  :-(

> Joe

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: simple... bug?
       [not found] <mailman.1649.1066115958.21628.help-gnu-emacs@gnu.org>
  2003-10-14  8:46 ` simple... bug? Alan Mackenzie
@ 2003-10-14  9:12 ` Joakim Hove
  2003-10-15  7:49 ` David Vanderschel
  2 siblings, 0 replies; 4+ messages in thread
From: Joakim Hove @ 2003-10-14  9:12 UTC (permalink / raw)



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     /
  \--------------------------------------------------------------------/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: simple... bug?
       [not found] <mailman.1649.1066115958.21628.help-gnu-emacs@gnu.org>
  2003-10-14  8:46 ` simple... bug? Alan Mackenzie
  2003-10-14  9:12 ` Joakim Hove
@ 2003-10-15  7:49 ` David Vanderschel
  2 siblings, 0 replies; 4+ messages in thread
From: David Vanderschel @ 2003-10-15  7:49 UTC (permalink / raw)


"Joe Corneli" <jcorneli@math.utexas.edu> wrote in message
news:mailman.1649.1066115958.21628.help-gnu-emacs@gnu.org...
>       Am I making some _extremely_ subtle mistake?

Not all that subtle, but easy to do.  What surprises
me is that Joe's mistake did not cause an error
message.  When I try Joe's function, I get

   Signaling: (void-variable list-of-squares)

Ie., the function does not even execute successfully.
I suspect that Joe had also created an instance of
list-of-squares at top level and its value is the one
being returned.

Joe, I think you could have easily resolved your
symptom had you tried stepping through the code with
edebug.  edebug is your friend!  :)

Regards,
  David V.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-10-15  7:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1649.1066115958.21628.help-gnu-emacs@gnu.org>
2003-10-14  8:46 ` simple... bug? Alan Mackenzie
2003-10-14  9:12 ` Joakim Hove
2003-10-15  7:49 ` David Vanderschel
2003-10-14  7:17 Joe Corneli

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.