General lisp format: call function with forward slash in name using ~ / directive
The format function in general lisp has a directive ~/name/
that allows you to call an arbitrary function to format a value. However, according to the docs, the name cannot have a / in. However, # / is a valid character in a symbol and therefore a function name. So, is there a way to escape the # / character in the function name passed to the ~ // directive?
Not really.
But you can write a function with a legal name that calls the function, which must then be passed with an argument. As in this example:
(defun foo (&rest args)
(apply (first (second args))
(first args)
(second (second args))
(cddr args)))
CL-USER 52 > (defun ba/r (&rest args) (print args))
BA/R
CL-USER 53 > (format t "~/foo/" '(ba/r (1 2)))
(#<LW-XP::XP-STRUCTURE 41C00D8B1B> (1 2) NIL NIL)
NIL
22.3.5.4 Tilde Slash: Call Function
~/name/
Custom functions can be called from a format string using the ~ / name / directive. The colon modifier, the at-sign modifier, and optionally many parameters can be specified using the ~ / name / directive parameter. name can be any arbitrary string that does not contain "/". All characters in the name are treated as if they were upper case. If a name contains a single colon (:) or double colon (: :), then everything up to but not including the first ":" or "::" is accepted to be a string that names the package. Everything after the first ":" or "::" (if any) is considered a string that names the character. the function matching the ~ / name / directive is obtained by searching for the symbol that has the specified name in the specified packaging. If the name does not contain ":" or "::",then the whole name line looked at the COMMON-LISP -USER package.
There are many characters that you cannot call using the tilde-slash directive; characters containing a #\/
are only one kind. Others are those with colons, or lowercase letters.