2

This is my first post so I apologize if I'm too vague or haven't done this correctly!

So I have an empty list L1 and two variables x and y which can be anything.

Say for example x = 10 and y = 20.

I would like the list L1 to start with x, and then I have a WHEN loop that add's y to the list every time it loops but I can't seem to get the list in the format I would like.

I currently have:

(let ((L1 x)))                  ; Adds x to the list

(loop

  (when (> n 10) (return))      ; While n < 10, add's y to the list.

  (setq L1 (list L1 y))

  (incf n)))

Which returns:

(10 20)
((10 20) 20)
(((10 20) 20) 20)
((((10 20) 20) 20) 20)
(((((10 20) 20) 20) 20) 20) ...

However I would like it to return:

( (10) (20) )
( (10) (20) (20) )
( (10) (20) (20) (20) )
( (10) (20) (20) (20) (20) )
( (10) (20) (20) (20) (20) (20) ) ...

Any ideas how I could do this?

Any help would be much appreciated!

0

1 Answer 1

2

For example:

(defun test ()
  (let ((x 10) (y 20) (n 10))
    (cons (list x)
          (loop 
            for i from 1 to n
            collect (list y)))))

then

(test)
=> ((10) (20) (20) (20) (20) (20) (20) (20) (20) (20) (20))

or

(defun test ()
  (let ((x 10) (y 20) (n 10))
    (cons (list x) (make-list n :initial-element (list y)))))
Sign up to request clarification or add additional context in comments.

2 Comments

Okay great, thanks! How do I set the list in your first example to L1?
(setq L1 (test)) or (setq L1 (cons (list x) ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.