From: Christian Zeller
Subject: Yet another FORMAT question: column width as parameter
Date:
Message-ID: <cpktij$ses$05$1@news.t-online.com>
hi
I have a print-cell function like this:
(defun print-cell (value)
(format t "~4<~A~>" value))
Now i'd like to define "4" as global variable or function parameter.
This obviously doesn't work:
(defun print-cell (value cell-width)
(format t "~~D<~A~>" cell-width value))
Ok. My current solution is this:
(defun print-cell (value cell-width)
(format t (format nil "~~~D<~~A~~>" cell-width) value))
I wonder if there is any better solution for this?
thanks,
chrizel
From: Wade Humeniuk
Subject: Re: Yet another FORMAT question: column width as parameter
Date:
Message-ID: <K%mvd.31202$Ya4.23842@edtnps84>
Christian Zeller wrote:
> hi
>
> I have a print-cell function like this:
>
> (defun print-cell (value)
> (format t "~4<~A~>" value))
>
> Now i'd like to define "4" as global variable or function parameter.
>
> This obviously doesn't work:
>
> (defun print-cell (value cell-width)
> (format t "~~D<~A~>" cell-width value))
>
> Ok. My current solution is this:
>
> (defun print-cell (value cell-width)
> (format t (format nil "~~~D<~~A~~>" cell-width) value))
>
> I wonder if there is any better solution for this?
CL-USER 20 > (defvar *cell-width* 4)
*CELL-WIDTH*
CL-USER 21 > (defun print-cell (value)
(format t "~V<~A~>" *cell-width* value))
PRINT-CELL
CL-USER 22 > (setf *cell-width* 20)
20
CL-USER 23 > (print-cell 10)
10
NIL
CL-USER 24 > (setf *cell-width* 4)
4
CL-USER 25 > (print-cell 10)
10
NIL
Wade
From: Svein Ove Aas
Subject: Re: Yet another FORMAT question: column width as parameter
Date:
Message-ID: <cpku6f$1p0$1@services.kq.no>
Christian Zeller wrote:
> hi
>
> I have a print-cell function like this:
>
> (defun print-cell (value)
> (format t "~4<~A~>" value))
>
> Now i'd like to define "4" as global variable or function parameter.
>
> This obviously doesn't work:
>
> (defun print-cell (value cell-width)
> (format t "~~D<~A~>" cell-width value))
>
> Ok. My current solution is this:
>
> (defun print-cell (value cell-width)
> (format t (format nil "~~~D<~~A~~>" cell-width) value))
>
> I wonder if there is any better solution for this?
>
Format calls the formatter function to compile your format-string; if the
string is constant this will be done at compile-time.
Now, you obviously can't do this if you want a global variable/function
parameter; however, you can call the formatter function yourself (and
supply the result to format instead of a format-string, if you like) if
you're going to use the same format-string more than once.
You probably will, and format *will* compile its format-string at runtime if
it has to. (That said, it isn't that expensive a compilation.)
From: Zach Beane
Subject: Re: Yet another FORMAT question: column width as parameter
Date:
Message-ID: <m34qiq7wro.fsf@unnamed.xach.com>
Svein Ove Aas <·········@aas.no> writes:
> Christian Zeller wrote:
>
>> hi
>>
>> I have a print-cell function like this:
>>
>> (defun print-cell (value)
>> (format t "~4<~A~>" value))
>>
>> Now i'd like to define "4" as global variable or function parameter.
[snip]
>
> Format calls the formatter function to compile your format-string; if the
> string is constant this will be done at compile-time.
I don't think this is specified behavior.
> Now, you obviously can't do this if you want a global variable/function
> parameter;
What do you mean by "global variable/function parameter"? You can can
use "V" to specify arguments to format directives in the argument
list.
(defun print-cell (value cell-width)
(format t "~V<~A~>" cell-width value))
Zach
From: Svein Ove Aas
Subject: Re: Yet another FORMAT question: column width as parameter
Date:
Message-ID: <cpl274$t4p$1@services.kq.no>
Zach Beane wrote:
> Svein Ove Aas <·········@aas.no> writes:
>
>> Christian Zeller wrote:
>>
>>> hi
>>>
>>> I have a print-cell function like this:
>>>
>>> (defun print-cell (value)
>>> (format t "~4<~A~>" value))
>>>
>>> Now i'd like to define "4" as global variable or function parameter.
> [snip]
>>
>> Format calls the formatter function to compile your format-string; if the
>> string is constant this will be done at compile-time.
>
> I don't think this is specified behavior.
>
Well, no, but it can generally be assumed. If it can't, the implementation
in question probably doesn't optimize much of anything.
>> Now, you obviously can't do this if you want a global variable/function
>> parameter;
>
> What do you mean by "global variable/function parameter"? You can can
> use "V" to specify arguments to format directives in the argument
> list.
>
> (defun print-cell (value cell-width)
> (format t "~V<~A~>" cell-width value))
>
Yep, that's a better way. In my defense, I'm still learning Lisp; I wasn't
planning on starting on Loop and Format just yet.
Svein Ove Aas wrote:
> Christian Zeller wrote:
>
>
>>hi
>>
>>I have a print-cell function like this:
>>
>>(defun print-cell (value)
>> (format t "~4<~A~>" value))
>>
>>Now i'd like to define "4" as global variable or function parameter.
>>
>>This obviously doesn't work:
>>
>>(defun print-cell (value cell-width)
>> (format t "~~D<~A~>" cell-width value))
Try ~v instead:
(loop for n from 10 to 15
do (format t "~&~v,,,'.<~a~>" n "hi, mom!"))
..hi, mom!
...hi, mom!
....hi, mom!
.....hi, mom!
......hi, mom!
.......hi, mom!
kt
--
Cells? Cello? Celtik?: http://www.common-lisp.net/project/cells/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
From: Szymon
Subject: Re: Yet another FORMAT question: column width as parameter
Date:
Message-ID: <85fz29bvar.fsf@eva.rplacd.net>
Christian Zeller <·······@gmail.com> writes:
Hi.
> hi
>
> I have a print-cell function like this:
>
> (defun print-cell (value)
> (format t "~4<~A~>" value))
>
> Now i'd like to define "4" as global variable or function parameter.
>
> This obviously doesn't work:
>
> (defun print-cell (value cell-width)
> (format t "~~D<~A~>" cell-width value))
>
> Ok. My current solution is this:
>
> (defun print-cell (value cell-width)
> (format t (format nil "~~~D<~~A~~>" cell-width) value))
>
> I wonder if there is any better solution for this?
Btw,
I wrote simple program to train/learn (myself) how to use format
directives.
(including ~V<...~>)
For example:
··············@<~A~>·····@<~A~>····@*~4:<~A~>······@·······@*~A~%"
Code: [ http://lisp.jogger.pl/comment.php?eid=80124 ].
Output: [ http://lisp.jogger.pl/comment.php?eid=80214 ].
(the data are geekcodes).
Regards, Szymon.