Lisp HUG Maillist Archive

FORMAT question

This is probably simple (as most things in FORMAT are), but I've not yet 
figured it out... I'd like to process a list, but print out the # of 
each item. For example:

 > (format t "~{~a~%~}" '(this that foobar))
THIS
THAT
FOOBAR

But, what I'd like to see is:
1. THIS
2. THAT
3. FOOBAR

Is there a simple method of making ~{ enumerate each item?

Thanks!
Jeff M.

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: FORMAT question

Do you specifically want FORMAT to do the heavy lifting for you (e.g. in 
the hope that it will avoid additional consing or something)?
I don't know of a clever format string, but you can get the same effect with

(format t "~{~d. ~a~%~}"
    (let ((index 0))
      (mapcan #'(lambda (elt) (list (incf index) elt))
              '(this that foobar))))
1. THIS
2. THAT
3. FOOBAR

Regards
Guy

On 08/04/2015 18:56, Jeffrey Massung wrote:
>
> This is probably simple (as most things in FORMAT are), but I've not yet
> figured it out... I'd like to process a list, but print out the # of
> each item. For example:
>
>  > (format t "~{~a~%~}" '(this that foobar))
> THIS
> THAT
> FOOBAR
>
> But, what I'd like to see is:
> 1. THIS
> 2. THAT
> 3. FOOBAR
>
> Is there a simple method of making ~{ enumerate each item?
>
> Thanks!
> Jeff M.
>
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html
>

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: FORMAT question

 An approach using recursive function for heavy lifting
based on table printing in  'LISP Second Edition' Winston 1984 p.116

;; take list ITEMS and character indent-char and print as numbered indented item elements
(defun numbered-list-print (items indent-char)
  (terpri)
  (numbered-list-print1 items (list indent-char) 0))

(defun numbered-list-print1 (items indent-list n)
  (cond ((null items) T)
    ((listp (car items))
     (numbered-list-print1 (car items)
                   (cons (first indent-list) indent-list)
                   0)
     (numbered-list-print1 (cdr items)
                   indent-list
                   n))
    (T
     (format t "~A~A. ~A~%" (coerce (rest indent-list) 'string) (+ n 1) (car items))
     (numbered-list-print1 (cdr items)
                   indent-list
                   (+ n 1)))))
gives
CL-USER> (numbered-list-print '("apple" "orange" ("dog" "cat" ("pig" "goat") "bird" "fish") "mango") #\*)

1. apple
2. orange
*1. dog
*2. cat
**1. pig
**2. goat
*3. bird
*4. fish
3. mango
T

Nigel


----- Original Message -----
From:
"Guy Footring" <Guy@Footring.net>

To:
"Jeffrey Massung" <massung@gmail.com>, "Lispworks HUG" <lisp-hug@lispworks.com>
Cc:

Sent:
Thu, 09 Apr 2015 00:03:42 +0100
Subject:
Re: FORMAT question



Do you specifically want FORMAT to do the heavy lifting for you (e.g. in
the hope that it will avoid additional consing or something)?
I don't know of a clever format string, but you can get the same effect with

(format t "~{~d. ~a~%~}"
(let ((index 0))
(mapcan #'(lambda (elt) (list (incf index) elt))
'(this that foobar))))
1. THIS
2. THAT
3. FOOBAR

Regards
Guy

On 08/04/2015 18:56, Jeffrey Massung wrote:
>
> This is probably simple (as most things in FORMAT are), but I've not yet
> figured it out.. I'd like to process a list, but print out the # of
> each item. For example:
>
> > (format t "~{~a~%~}" '(this that foobar))
> THIS
> THAT
> FOOBAR
>
> But, what I'd like to see is:
> 1. THIS
> 2. THAT
> 3. FOOBAR
>
> Is there a simple method of making ~{ enumerate each item?
>
> Thanks!
> Jeff M.
>
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworkscom
> http://www.lispworks.com/support/lisp-hug.html
>

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Updated at: 2020-12-10 08:33 UTC