Re: Opposite of ~^ FORMAT Directive



vy <volkan.yazici@xxxxxxxxx> writes:

Is it possible to make FORMAT print a phrase just in the first element
of a ~{...~} list iteration. (Like opposite of ~^.)

I think the reason there's not is that often one just reorganizes the
text slightly, pushing the first part out of the loop to the left. e.g.,

(defun foo (x) (format t "First ~{~A~^, then ~}." x))
=> FOO

(foo '(a b c d))
First A, then B, then C, then D.
=> NIL

If the case of (foo '()) is something you worry about, then use:

(defun foo (x) (format t "~:[None~;~:*First ~{~A~^, then ~}~]." x))
=> FOO

(foo '())
None.
=> NIL

If you insist on pushing things into the loop, about the only trick that
comes to mind, and it's really an ugly trick--but worth knowing, is to
use some sort of circular list thing, as in:

(defun circular-list (&rest args)
(let ((x (copy-list args)))
(nconc x x)))
=> CIRCULAR-LIST

(setq *print-circle* t)
=> T

; test it out
(circular-list 'a 'b 'c)
=> #1=(A B C . #1#)

(defun foo (x)
(format t "~:{~:[First~:;then~] ~A~:^, ~}."
(mapcar #'list ;this mapcar will stop as long as x is not circular!
(cons nil (circular-list t))
x)))
=> FOO

(foo '(a b c d))
First A, then B, then C, then D.
=> NIL

Then again, I don't use every last feature of FORMAT, so maybe someone else
remembers another feature I'm forgetting.

But you know, you're not really _required_ to put everything in one FORMAT
string. It is ok to use conventional loops, multiple calls to FORMAT, etc.
Sometimes, it's a lot more readable that way, too.
.



Relevant Pages

  • Re: import parent
    ... foo imports bar -- how does bar access foo? ... Given oof is such an instance, ... foo imports bar which imports foo is called a "circular import". ... if what you want to do really would involve a circular ...
    (comp.lang.python)
  • Re: popold
    ... NIL ... FOO ... Elapsed time = 0:00:01 ... Page faults ...
    (comp.lang.lisp)
  • Re: NIL as :key to ASSOC, INTERSECTION, POSITION, &c.
    ... I wouldn't have thought think that providing NIL as a:key ... (defun foo (&key bar) ... Above you would need to check for each keyword argument ...
    (comp.lang.lisp)
  • Re: Scoping Question
    ... * (setf foo 3) ... Warning: This variable is undefined: ... WARN is an external symbol in the KEYWORD package. ... NIL -- Never declare the variable, ...
    (comp.lang.lisp)
  • Re: surprizing behaviour (at least for a c/c++ veteran)
    ... creates a literal vector in the same manner that also ... The variable FOO is defined but never used. ... contstants, so you are seeing the correct results, but your code is non-conforming as it is still making modifications to contstant data, and the nasal demons should be forthcoming any minute now. ...
    (comp.lang.lisp)