[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

COMMENT 2 on the book CL Recipes A Problem-Solution Approach by Edi Weitz

kodifik

6/5/2016 5:08:00 PM

;;; COMMENT 2 on the book CL Recipes A Problem-Solution Approach by Edi Weitz
;;; chap.3-12 pag.83
;;; Format can be used inside format.
;;; The example function can thus become a mere one-liner:
(defun join (separator list) (format nil (format nil "~~{~~A~~^~A~~}" separator) list))
;;; USE: (join ", " (list "C" "C++" "C#"))

;;; Recipe :
;;; 1- Start from a working controlstring. In the example "~{~A~^, ~}"
;;; 2- Convert it to an equivalent expr. (format nil "~~{~~A~~^, ~~}")
;;; simply doubling each tilde char.
;;; 3- Transform again into (format nil "~~{~~A~~^~A~~}" variable)
;;; where variable is ", " but could now be changed.
;;; 4- Optionally check every step at the repl.
;;; Full potential of FORMAT can (only?) be attained this way.

2 Answers

Siebe de Vos

6/5/2016 5:49:00 PM

0

On 2016-06-05 19:08, kodifik@gmail.com wrote:
> ;;; COMMENT 2 on the book CL Recipes A Problem-Solution Approach by Edi Weitz
> ;;; chap.3-12 pag.83
> ;;; Format can be used inside format.
> ;;; The example function can thus become a mere one-liner:
> (defun join (separator list) (format nil (format nil "~~{~~A~~^~A~~}" separator) list))
> ;;; USE: (join ", " (list "C" "C++" "C#"))
>
> ;;; Recipe :
> ;;; 1- Start from a working controlstring. In the example "~{~A~^, ~}"
> ;;; 2- Convert it to an equivalent expr. (format nil "~~{~~A~~^, ~~}")
> ;;; simply doubling each tilde char.
> ;;; 3- Transform again into (format nil "~~{~~A~~^~A~~}" variable)
> ;;; where variable is ", " but could now be changed.
> ;;; 4- Optionally check every step at the repl.
> ;;; Full potential of FORMAT can (only?) be attained this way.
>

This may not work as expected: (join "~" '("a" "a"))

Siebe

kodifik

6/6/2016 6:40:00 PM

0

On Sunday, 5 June 2016 19:49:22 UTC+2, Siebe de Vos wrote:
> On 2016-06-05 19:08, kodifik@gmail.com wrote:
> > ;;; COMMENT 2 on the book CL Recipes A Problem-Solution Approach by Edi Weitz
> > ;;; chap.3-12 pag.83
> > ;;; Format can be used inside format.
> > ;;; The example function can thus become a mere one-liner:
> > (defun join (separator list) (format nil (format nil "~~{~~A~~^~A~~}" separator) list))
> > ;;; USE: (join ", " (list "C" "C++" "C#"))
> >
> > ;;; Recipe :
> > ;;; 1- Start from a working controlstring. In the example "~{~A~^, ~}"
> > ;;; 2- Convert it to an equivalent expr. (format nil "~~{~~A~~^, ~~}")
> > ;;; simply doubling each tilde char.
> > ;;; 3- Transform again into (format nil "~~{~~A~~^~A~~}" variable)
> > ;;; where variable is ", " but could now be changed.
> > ;;; 4- Optionally check every step at the repl.
> > ;;; Full potential of FORMAT can (only?) be attained this way.
> >
>
> This may not work as expected: (join "~" '("a" "a"))
>
> Siebe

Yes indeed.
A two-liner, then...