Lisp string formatting with named parameters

后端 未结 1 1180
借酒劲吻你
借酒劲吻你 2021-01-11 13:27

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         


        
1条回答
  •  渐次进展
    2021-01-11 14:14

    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.

    0 讨论(0)
提交回复
热议问题