Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp Format Procedure

Im starting to program in Lisp and having an hard time with the Format function.

My objective is to print a list of integer sublists as N integers for line. For example:

'((1 2 3)
(4 5 6)
(7 8 9))

should be printed as

1 2 3
4 5 6
7 8 9

I tried using iteration in the format procedure, but I failed.

What I wrote was:

(format t "~{~S ~}" list)

But with this I get the sublists as "(1 2 3)" instead of "1 2 3", so i tried:

(format t "~:{ ~S ~}" list)

this time I got into the sublists but only printed the first element, so I stepped in and re-wrote the function to:

(format t "~:{ ~S ~S ~S ~}" list)

It works for sublists with 3 elements, but how can I make it work for n elements?

Thanks!

like image 648
rfsbraz Avatar asked Jan 21 '23 01:01

rfsbraz


1 Answers

(format t "~{~%~{~A~^ ~}~}"  '((1 2 3) (4 5 6) (7 8 9)))

prints

1 2 3
4 5 6
7 8 9
like image 197
Rainer Joswig Avatar answered Jan 28 '23 03:01

Rainer Joswig