30 lines
907 B
Common Lisp
30 lines
907 B
Common Lisp
;;; VECTORS 3: STRINGS ARE VECTORS (solution)
|
|
|
|
(defun first-letter (w)
|
|
(aref w 0))
|
|
|
|
(defun retag (w c)
|
|
(let ((s (make-array (length w) :element-type 'character
|
|
:initial-contents w)))
|
|
(setf (aref s (- (length s) 1)) c)
|
|
s))
|
|
|
|
(defun letter-run (n c)
|
|
(make-array n :element-type 'character :initial-element c))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check (vectorp "cat"))
|
|
(check-equal #\c (first-letter "cat"))
|
|
(check-equal #\a (first-letter "apple"))
|
|
(check-equal "cab" (retag "cat" #\b))
|
|
(check-equal "suns" (retag "sung" #\s))
|
|
(check (let ((w "cat"))
|
|
(retag w #\b)
|
|
(equal "cat" w)))
|
|
(check-equal "aaa" (letter-run 3 #\a))
|
|
(check-equal "xx" (letter-run 2 #\x))
|