If you want to do it with format
- the problem is to access first and second element of the alist using format
directives. I didn't found how I could access them inside a format
directive.
However, in such regularly formed structures like an alist, one could flatten the list first and then let format
-looping directive consume two elements per looping - then one consumes the pair.
Since the famous :alexandria
library doesn't count as dependency in Common Lisp world, one could directly use alexandria:flatten
:
(defparameter *al* '((24 . 23) (9 . 6)))
(ql:quickload :alexandria) ;; import alexandria library
(format nil "~{~a/~a~^ ~}" (alexandria:flatten *al*))
;; => "24/23 9/6"
nil
return as string
~{ ~}
loop over the list
~a/~a
the fraction
~^
empty space between the elements but not after last element
flatten
by the way without :alexandria
-"dependency" would be in this case:
(defun flatten (l)
(cond ((null l) nil)
((atom l) (list l))
(t (append (flatten (car l)) (flatten (cdr l))))))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…