(beginnerQ) format in loop

Started by chang, May 24, 2006, 11:11:22 PM

Previous topic - Next topic

chang

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

m i c h a e l

#1
Quote from: "chang"When i do this


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

(dolist (a '(1 3 5 7))
    (format "%d" a))


Quote from: "chang"newlisp gives me this


2
4
6
8


Quote from: "chang"I thought this should be like


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:


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


> [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:


> [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" _


Quote from: "chang"Sorry for a dull question.

It's those shiny questions that can be the worst :-)





m i c h a e l

Lutz

#2
... make sure [cmd] [/cmd] tags are on their own separate lines when pasting multi-line code to work correctly:



> [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

m i c h a e l

#3
Quote from: "Lutz"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

chang

#4
Thanks, m i c h a e l !



You solved my 4 hour blunder  :)



Have a nice day !

m i c h a e l

#5
Quote from: "chang"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