Page 1 of 1

(beginnerQ) format in loop

Posted: Thu May 25, 2006 6:11 am
by chang

Code: Select all

(silent)
(dolist (a '(2 4 6 8))
    (println a))

(dolist (a '(1 3 5 7))
    (format "%d" a))
When i do this, newlisp gives me this,

2
4
6
8


I thought this should be like,

2
4
6
8
"1" "3" "5" "7"

Am I missing something in lisp-way?

Sorry for a dull question.

Posted: Thu May 25, 2006 3:57 pm
by m i c h a e l
chang wrote:When i do this

Code: Select all

(silent)
(dolist (a '(2 4 6 8))
    (println a))

(dolist (a '(1 3 5 7))
    (format "%d" a))
chang wrote:newlisp gives me this

Code: Select all

2
4
6
8
chang wrote:I thought this should be like

Code: Select all

2
4
6
8
"1" "3" "5" "7"
Here are some of the things I see here. silent should be wrapped around the output you want to suppress, as in:

Code: Select all

> (silent (dolist (a (sequence 2 8 2)) (println a)))
2
4
6
8
_; note: the prompt will be missing. press [enter] to see the prompt again.
Also, format returns a string but does not print output. Within the dolist as you have it, the result is just being thrown away with each iteration. Placing a print before format would give you:

N.B. Please see Lutz's comment directly following this post for a correction to the code samples below.

Code: Select all

> [cmd](silent 
	(dolist (a (sequence 2 8 2)) (println a)) 
	(dolist (a (sequence 1 7 2)) (print (format "%d" a))))
[/cmd]
2
4
6
8
1357_
This is close, but you had expected the results to be strings, so you could also do this:

Code: Select all

> [cmd](silent 
	(dolist (a (sequence 2 8 2)) (println a)) 
	(dolist (a (sequence 1 7 2)) (print (format ""%d" " a))))
[/cmd]
2
4
6
8
"1" "3" "5" "7"	_
chang wrote:Sorry for a dull question.
It's those shiny questions that can be the worst :-)


m i c h a e l

Posted: Thu May 25, 2006 5:04 pm
by Lutz
... make sure [cmd] [/cmd] tags are on their own separate lines when pasting multi-line code to work correctly:

Code: Select all

> [cmd]
(silent 
   (dolist (a (sequence 2 8 2)) (println a)) 
   (dolist (a (sequence 1 7 2)) (print (format "%d" a)))) 
[/cmd]
2
4
6
8
1357
Lutz

Posted: Thu May 25, 2006 6:56 pm
by m i c h a e l
Lutz wrote:make sure [cmd] [/cmd] tags are on their own separate lines when pasting multi-line code to work correctly:
Whoops! Thank you, Lutz.


m i c h a e l

Posted: Fri May 26, 2006 1:05 am
by chang
Thanks, m i c h a e l !

You solved my 4 hour blunder :)

Have a nice day !

Posted: Sun Jun 04, 2006 5:47 am
by m i c h a e l
chang wrote:Thanks, m i c h a e l !
A belated "You're welcome" to you, chang :-)

Welcome to the wonderful world of newLISP!


m i c h a e l