Plaster
New
List
Login
common-lisp
default
anonymous
2021.07.21 08:43:38
;; Please ignore the fact that I use DOLIST instead of LOOP...COLLECT ;; and that there are too many lexical variables here. This is a ;; contrived example to compare two approaches of writing code with ;; too many lexical variables. (defun ex1 (articles) "Given a list of articles (each article is a string), extract the first line (title) from each article, capitalize it, and return a list of titles along with the number of titles returned." (let ((count (length articles)) (newline-position) (title) (titles)) (dolist (article articles) (setf newline-position (position #\Newline article)) (setf title (subseq article 0 newline-position)) (push title titles)) (values count titles))) (defun ex2 (articles) "Same as previous function." (let ((count (length articles)) (titles)) (dolist (article articles) (let* ((newline-position (position #\Newline article)) (title (subseq article 0 newline-position))) (push title titles))) (values count titles))) (defun ex2 (articles)) (ex1 (list (format nil "cities~%london~%edinburgh~%cardiff~%") (format nil "fruits~%apple~%pear~%banana~%") (format nil "people~%alice~%bob~%carol~%"))) ;; => 3, ("people", "fruits", "cities") (ex2 (list (format nil "cities~%london~%edinburgh~%cardiff~%") (format nil "fruits~%apple~%pear~%banana~%") (format nil "people~%alice~%bob~%carol~%"))) ;; => 3, ("people", "fruits", "cities")
Raw
Annotate
Repaste
Edit