From: pdn Date: Sat, 15 Feb 2014 08:19:15 +0000 (-0800) Subject: Add MAKE-LIST. X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=f0a14f3e363b6d0cf1c30635f3d732b7bc0b15a4;p=jscl.git Add MAKE-LIST. --- diff --git a/src/list.lisp b/src/list.lisp index 7a06dd7..368d098 100644 --- a/src/list.lisp +++ b/src/list.lisp @@ -197,6 +197,17 @@ (when (eql tail object) (return-from tailp t)))) +(defun make-list (size &key (initial-element nil)) + "Create a list of size `size` of `initial-element`s." + (when (< size 0) + (error "Size must be non-negative")) + (let ((newlist)) + (do ((i 0)) + ((= i size)) + (push initial-element newlist) + (incf i)) + newlist)) + (defun map1 (func list) (with-collect (while list diff --git a/tests/list.lisp b/tests/list.lisp index db44b9a..9580752 100644 --- a/tests/list.lisp +++ b/tests/list.lisp @@ -244,3 +244,7 @@ (test (equal (let ((thing '())) (butlast thing 2)) '())) (test (equal (let ((thing '(1 2))) (butlast thing 2)) '())) (test (equal (let ((thing '())) (butlast thing 0)) '())) + +;; MAKE-LIST +(test (equal (make-list 5) '(nil nil nil nil nil))) +(test (equal (make-list 3 :initial-element 'rah) '(rah rah rah)))