Skip to content

Commit

Permalink
Added solution using loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
informatimago committed Apr 11, 2017
1 parent d1fc0cd commit 34f421c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions p15.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,37 @@ P15 (**) Replicate the elements of a list a given number of times.
(append (repli-one (first list) count)
(repli (rest list) count)))))

;; Using loop:

(defun repli (list n)
(when list
(loop
:with current-element
:with i := 0
:until (and (null list) (zerop i))
:if (zerop i)
:do (setf i n
current-element (pop list) )
:else :do (decf i)
:and :collect current-element
:end)))

;; (values (repli '(a b c) 3)
;; (repli '(a b c) 1)
;; (repli '(a b c) 0)
;; (repli '(a) 3)
;; (repli '(a) 1)
;; (repli '(a) 0)
;; (repli '() 3)
;; (repli '() 0))
;; (a a a b b b c c c)
;; (a b c)
;; nil
;; (a a a)
;; (a)
;; nil
;; nil
;; nil


;;;; THE END ;;;;

0 comments on commit 34f421c

Please sign in to comment.