Duplicate elements in general lisp

I am trying to create a function with two arguments x and y that creates a list of y times repeated elements X , but im confused about how to do this, which or which method to use, I think the list compression can do, but I want more short and simple method for example, I want my simple code to be like this

 if y = 4 and x = 7 result is list of elements (7, 7, 7, 7) 

how can i get around any ideas? books or anything that will give me the key, I tried searching, but I had no luck.

+4
source share
2 answers

Try this out in the Scheme, but the general idea should be simple enough to translate to Common Lisp:

 (define (repeat xy) (if (zero? y) null (cons x (repeat x (sub1 y))))) 

EDIT:

Now in Common Lisp:

 (defun repeat (xy) (if (zerop y) nil (cons x (repeat x (1- y))))) 
+1
source

You can use make-list with the key of the initial element:

 CL-USER> (make-list 10 :initial-element 8) (8 8 8 8 8 8 8 8 8 8) 

While a good example of how you can code such a function yourself is provided by Óscar's answer.

+18
source

Source: https://habr.com/ru/post/1446785/


All Articles