Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp string formatting with named parameters

Is there a way in Lisp to format a string using named parameters?

Perhaps something with association lists like

(format t "All for ~(who)a and ~(who)a for all!~%" ((who . "one")))

in order to print "All for one and one for all".

Similar to this python question, or this scala one, or even c++, but in Lisp.

If this functionality isn't in the language, does anyone have any cool functions or macros that could accomplish the same thing?

like image 619
wutch Avatar asked May 08 '16 16:05

wutch


1 Answers

Use CL-INTERPOL.

(cl-interpol:enable-interpol-syntax)

String interpolation

For simple cases, you don't need FORMAT:

(lambda (who) #?"All for $(who) and $(who) for all!")

Then:

(funcall * "one")
=> "All for one and one for all!"

Interpret format directives

If you need to format, you can do:

(setf cl-interpol:*interpolate-format-directives* t)

For example, this expression:

(let ((who "one"))
  (princ #?"All for ~A(who) and ~S(who) for all!~%"))

... prints:

All for one and "one" for all!

If you are curious, the above reads as:

(LET ((WHO "one"))
  (PRINC
    (WITH-OUTPUT-TO-STRING (#:G1177)
      (WRITE-STRING "All for " #:G1177)
      (FORMAT #:G1177 "~A" (PROGN WHO))
      (WRITE-STRING " and " #:G1177)
      (FORMAT #:G1177 "~S" (PROGN WHO))
      (WRITE-STRING " for all!" #:G1177))))

Alternate reader function

Previously, I globally set *interpolate-format-directives*, which interprets format directive in all interpolated strings. If you want to control precisely when format directives are interpolated, you can't just bind the variable temporarily in your code, because the magic happens at read-time. Instead, you have to use a custom reader function.

(set-dispatch-macro-character
 #\#
 #\F
 (lambda (&rest args)
   (let ((cl-interpol:*interpolate-format-directives* t))
     (apply #'cl-interpol:interpol-reader args))))

If I reset the special variable to its default value NIL, then strings where directives are formatted are prefixed with #F, whereas normal interpolated ones use the #? syntax. If you want to change readtables, have a look at named readtables.

like image 77
coredump Avatar answered Sep 29 '22 11:09

coredump