Monday, May 17, 2004

Discovering Format Directives

So I was just reading some Common Lisp information on the web when I saw these really cool code examples in Peter Siebel's excellent book "Practical Common Lisp" that uses format. I have used format but had not realized how cool it really is.

The first very interesting example used the "~{" and "~}" directive which lets us use nested lists as parameters to format. I decided to test this on my own for an understanding


[30]> (format t "~{~{~a~%~a~%~}~}" '((a b)))
A
B
NIL


Looks good. So I tried


[34]> (format t "~{~{~a~%~a~%~}~}" '((a b) (c d) (e f) (g h)))
A
B
C
D
E
F
G
H
NIL


Even better. So if it supports nested lists, then it should obviously allow more nesting and so I tried


[35]> (format t "~{~{~{~a~%~a~%~}~}~}" '(((a b))))
A
B
NIL
[36]> (format t "~{~{~{~a~%~a~%~}~}~}" '(((a b) (c d))))
A
B
C
D
NIL


Yup. As usual in impressive Lisp style, my REPL responds as expected. So I was pretty happy about learning something new, thanks to Peter Siebel, I read further down and finally ended up in the footnotes section where I found this gem.


[37]> (format nil "~r" 1606938044258990275541962092)
"one octillion, six hundred and six septillion, nine hundred and thirty-eight sextillion,
forty-four quintillion, two hundred and fifty-eight quadrillion, nine hundred and ninety
trillion, two hundred and seventy-five billion, five hundred and forty-one million, nine
hundred and sixty-two thousand, ninety-two"


Holy mother of batman! That is bloody impressive for a function I have so far only used for printing. Note to self -> ALWAYS READ FOOTNOTES. Anyway, curious about how much it would take, I figured that Clisp can take a max of One Hundred Vigintillion which is 10^66. So guess what I had to know all the intermediate names.


[38]> (do ((x (expt 10 9) (* x 1000)))
((>= x (expt 10 66)))
(format t "~r = ~a~%" x x))
one billion = 1000000000
one trillion = 1000000000000
one quadrillion = 1000000000000000
one quintillion = 1000000000000000000
one sextillion = 1000000000000000000000
one septillion = 1000000000000000000000000
one octillion = 1000000000000000000000000000
one nonillion = 1000000000000000000000000000000
one decillion = 1000000000000000000000000000000000
one undecillion = 1000000000000000000000000000000000000
one duodecillion = 1000000000000000000000000000000000000000
one tredecillion = 1000000000000000000000000000000000000000000
one quattuordecillion = 1000000000000000000000000000000000000000000000
one quindecillion = 1000000000000000000000000000000000000000000000000
one sexdecillion = 1000000000000000000000000000000000000000000000000000
one septendecillion = 1000000000000000000000000000000000000000000000000000000
one octodecillion = 1000000000000000000000000000000000000000000000000000000000
one novemdecillion = 1000000000000000000000000000000000000000000000000000000000000
one vigintillion = 1000000000000000000000000000000000000000000000000000000000000000
NIL


I had a "for" loop macro construct that I used to use for simple stuff but realized I need to modify it after this example and had to get into a "DO". I would appreciate if someone can show me how to do this with the "LOOP" macro.

2 Comments:

Blogger download said...

Try this:

Basically, the format string re-uses the argument X for both the ~R and ~A arguments. And, yes, CL's Format is *awesome*! :)

(loop for n from 9 to 63 by 3
         for x = (expt 10 n)
         do (format t "~r = ~:*~A~%~%" x))

8:55 PM  
Blogger download said...

Try this:

Basically, the format string re-uses the argument X for both the ~R and ~A arguments. And, yes, CL's Format is *awesome*! :)

(loop for n from 9 to 63 by 3
         for x = (expt 10 n)
         do (format t "~r = ~:*~A~%~%" x))

8:56 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home