(beginnerQ) format in loop

Q&A's, tips, howto's
Locked
chang
Posts: 2
Joined: Thu May 25, 2006 6:05 am

(beginnerQ) format in loop

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

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post 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
Last edited by m i c h a e l on Fri May 26, 2006 8:42 pm, edited 1 time in total.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post 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

chang
Posts: 2
Joined: Thu May 25, 2006 6:05 am

Post by chang »

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
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post 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

Locked