gilnari
gilnari

Reputation: 1

Add to a list that is in a Loop using LISP

I'm very new to Lisp and I'm having issues appending to a list in a loop. If my list is missionaries and I pass in a value of cnt (number of times to loop) I need to take M plus current incremententor of the loop, concant and add it to the missionary list. So as it loop I get M1 M2 M3 M4 etc. . Thing is, it just not append. I also tried push but then it runs on as M1 M1 M1 never ending loop.

(defparameter missionaries (list nil))
(setq x 1)
(setq cnt 20)
(loop
    (format t "~d ~%" (intern (format nil "~a~a" "M" x)))
    (append *missionaries* (intern (format nil "~a~a" "M" x)))
    ;(push (intern (format nil "~a~a" "M" x)) *missionaries*)
    (setq x (+ x 1))
    (when (> x cnt) (return x))
)   

Upvotes: 0

Views: 365

Answers (1)

Kaz
Kaz

Reputation: 58500

(defparameter *missionaries*
              (loop for i from 1 to 20
                    collect (intern (format nil "M~a" i))))

Upvotes: 2

Related Questions