Plaster
New
List
Login
text
default
anonymous
2021.02.03 17:03:22
-*- org -*- Common Lisp FORMAT Idioms * Boolean value tags: ON/OFF, Open/Closed, etc (format t "The switch is: ~:[OFF~;ON~]~%" t) * Print only if true: (format t "It's ~:[~;very ~]hot here.~%" (> temp 38)) (format t "You have: ~@[~a ~]~@[~a ~]~@[~a ~].~%" 'cats nil 'dogs) * Numbered choices: (format t "~[Siamese~;Manx~;Persian~] Cat" 1) * Numbered choices, with default: (format t "~[Siamese~;Manx~;Persian~:;Alley~] Cat" 5) * Different style based on number of arguments: (format t "And then there ~#[were none~;was just ~s~;was ~s and ~s~:;were ~@{~#[~;and ~]~a~^, ~}~]." ...) * List with internal spaces (or commas) (format t "~{~a~^ ~}~%" '(foo bar baz quux)) * List with internal spaces, not printing NIL elements. (format t "|~{~@[~a~^ ~]~^~}|~%" '(foob nil arb nil azz)) * Pluralization: (format t "You have ~d cat~:P.~%" 1) (format t "You see ~d kitt~:@P here." 23) ; y vs. ies * Justification: ; to 80 columns (format t "~{~<~%~1,80:;~a~> ~}" (mapcar #'package-name (list-all-packages))) * Centering (format t "~v:@<~a~>" 20 "Hey") * Spelled out numbers: (format t "~r~%" 1998) * Roman numerals: (format t "~@r~%" 1998) * Character repated N times. : (format t "~v,,,va " N #\- #\-) or if the character is constant, you could include in the string : (format nil "~v,,,'-a" N #\-) or another non-format way that may be quicker but more memory pressure : (make-string 4 :initial-element #\x) * Number padded with zeros (format t "~8,'0d" number) (format t "~v,'0d" pad-width number) * Apply format to args (apply #'format stream string arguments) == (format stream "~1{~:}" string arguments)
Raw
Annotate
Repaste
Edit