Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing..

3 views
Skip to first unread message

Anssi Purola

unread,
Jul 25, 1999, 3:00:00 AM7/25/99
to

Why this doesn't print numbers from 1 to 100? It only prints '99'.


(defun loop-function()
( do ((i 0 (+ i 1))) (>= i 99) (print i)))

And what's right way to print something?

(setq temp 10)
(print temp)

It shoul work, but it prints '10' twice.

BTW, Sorry about my English..

Erik Naggum

unread,
Jul 25, 1999, 3:00:00 AM7/25/99
to
* Anssi Purola <anssi....@pp.inet.fi>

| Why this doesn't print numbers from 1 to 100? It only prints '99'.
|
| (defun loop-function()
| ( do ((i 0 (+ i 1))) (>= i 99) (print i)))

to make this slightly more readable:

(do ((i 0 (+ i 1)))


(>= i 99)
(print i)))

the problem here is that (>= i 99) is a malformed termination clause.
somehow, >= as a variable is is true in your image. I suspect that this
comes from something you have done previously. if you start up a new
image and try to evaluate it again, you will get an error. (if you
don't, you have a really weird Lisp environment.)

try using ((>= i 99)), instead. the termination clause is a list whose
head is a form and whose tail is a body to be evaluated when the form
evaluates to true, returning the value of the last form of the body.

| And what's right way to print something?
|
| (setq temp 10)
| (print temp)
|
| It shoul work, but it prints '10' twice.

it should print 10 three times: (1) the return value of the SETQ form,
(2) the value of TEMP in the PRINT function, and (3) the return value of
the PRINT form.

this is because your Lisp system does (loop (print (eval (read)))) or
something close to it while it reads and evaluates input from you.

#:Erik
--
suppose we blasted all politicians into space.
would the SETI project find even one of them?

Gareth McCaughan

unread,
Jul 25, 1999, 3:00:00 AM7/25/99
to
Anssi Purola wrote:

> Why this doesn't print numbers from 1 to 100? It only prints '99'.
>
>
> (defun loop-function()
> ( do ((i 0 (+ i 1))) (>= i 99) (print i)))

Your termination condition is wrong. Take a really careful look
at the description of the syntax for DO and try to match it up
against what you gave it, piece by piece. You'll soon spot the
problem.

> And what's right way to print something?
>
> (setq temp 10)
> (print temp)
>
> It shoul work, but it prints '10' twice.

The first time it prints 10 because you told it to. The second time
it prints 10 because the Lisp system always displays the value(s)
returned from the expressions you feed it. The two "10"s are really
being printed by different entities: the first one by your program
and the second one by the "read-eval-print loop".

--
Gareth McCaughan Gareth.M...@pobox.com
sig under construction

Kenneth P. Turvey

unread,
Jul 25, 1999, 3:00:00 AM7/25/99
to
On Sun, 25 Jul 1999 15:39:20 GMT,
Anssi Purola <anssi....@pp.inet.fi> wrote:
>
>Why this doesn't print numbers from 1 to 100? It only prints '99'.
>
>
>(defun loop-function()
> ( do ((i 0 (+ i 1))) (>= i 99) (print i)))

This should be:

(do ((i 0 (+ i 1)))
((> i 99) t)
(print i))

The problem was with your end test. In CMUCL the function you provided
just gives an error.

>And what's right way to print something?
>
>(setq temp 10)
> (print temp)
>
>It shoul work, but it prints '10' twice.

No, the function prints '10' once and then returns the value that it
printed. This value is then printed by the reader so you know what the
return value was.

--
Kenneth P. Turvey <ktu...@SprocketShop.com>
----------------- http://www.tranquility.net/~kturvey

One of the advantages of being disorderly is that one is constantly
making exciting discoveries.
-- A. A. Milne

0 new messages