Generating lists of given length
Posted: Mon Sep 26, 2011 4:16 pm
Hello.How can I generate all possible lists of length n,consisting of elements "a" and "b".For example,if n=5 there must be 32 lists of length 5,consisting of "a" and "b".
Discuss and learn Lisp programming of all dialects. NOTICE: Site locked. No new users or posts.
https://lispforum.com/
Code: Select all
(defun ota(c d)(if (null c) (list d)
(if (=(list-length c)1)(list (append (car c)(list (car d))))
(cons (append (car c) (list (car d)))
(ota (cdr c) d)) )))
(defun m(c d) (if (=(list-length d)1)(ota c d)
(append (m c (list (car d))) (m c (cdr d)))))
(defun gen(a n) (if(= n 0) nil (if(= n 1)(list (list (car a)) (cdr a) )
(m (gen a (1- n))a))))