Hi,
I would like to format something differently depending on its type.
For example,
(defun foo (value precision)
(format nil "~[~,vF (float)~;~*~D (integer)~;~*~A (other)~]"
(typecase value
(float 0)
(integer 1)
(t 2))
precision
value))
CL> (foo 1 4)
"1 (integer)"
CL> (foo pi 4)
"3.1416 (float)"
CL> (foo "bar" 4)
"bar (other)"
I did it with ~[ and typecase, but I wonder if format could condition
on type, leading to a more compact version.
Thanks,
Tamas
On 2007-07-07 15:04:26 +0200, Tamas Papp <······@gmail.com> said:
> Hi,
>
> I would like to format something differently depending on its type.
> For example,
>
> (defun foo (value precision)
> (format nil "~[~,vF (float)~;~*~D (integer)~;~*~A (other)~]"
> (typecase value
> (float 0)
> (integer 1)
> (t 2))
> precision
> value))
>
> CL> (foo 1 4)
> "1 (integer)"
> CL> (foo pi 4)
> "3.1416 (float)"
> CL> (foo "bar" 4)
> "bar (other)"
>
> I did it with ~[ and typecase, but I wonder if format could condition
> on type, leading to a more compact version.
>
> Thanks,
>
> Tamas
You could also do:
(format
nil
(typecase bar
(float ".....")
(integer "...."))
bar)
--
http://lispm.dyndns.org/