From f0a14f3e363b6d0cf1c30635f3d732b7bc0b15a4 Mon Sep 17 00:00:00 2001 From: pdn Date: Sat, 15 Feb 2014 00:19:15 -0800 Subject: [PATCH] Add MAKE-LIST. --- src/list.lisp | 11 +++++++++++ tests/list.lisp | 4 ++++ 2 files changed, 15 insertions(+) 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))) -- 1.7.10.4