39 lines
1.3 KiB
Common Lisp
39 lines
1.3 KiB
Common Lisp
;;; STRINGS 7: NEWLINES AND COLUMNS
|
|
;;;
|
|
;;; ~% ALWAYS breaks the line. ~& breaks one only when you are not
|
|
;;; at the start of a line already - fresh line, not newline - so a
|
|
;;; brand-new string gets nothing:
|
|
;;;
|
|
;;; (format nil "~&~a" "hi") => "hi"
|
|
;;;
|
|
;;; ~v takes the field width from the arguments, and @ pads on the
|
|
;;; left - right-justified:
|
|
;;;
|
|
;;; (format nil "~v@a" 6 "ada") => " ada"
|
|
|
|
;; TODO: S on a fresh line: nothing before it when the line is
|
|
;; already new.
|
|
(defun add-line (s)
|
|
(format nil "~%~a" s))
|
|
|
|
;; TODO: NAME right-justified in a field WIDTH wide.
|
|
(defun right-column (name width)
|
|
(format nil "~va" width name))
|
|
|
|
;; TODO: A receipt row: ITEM in an 8-wide column, PRICE right-
|
|
;; justified in 4.
|
|
(defun receipt (item price)
|
|
(format nil "~a ~a" item price))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal "hi" (add-line "hi"))
|
|
(check-equal "parrot" (add-line "parrot"))
|
|
(check-equal " ada" (right-column "ada" 6))
|
|
(check-equal "hi" (right-column "hi" 2))
|
|
(check-equal "tea 2" (receipt "tea" 2))
|
|
(check-equal "coffee 3" (receipt "coffee" 3))
|