I've started learning LISP again after several unsuccessful attempts, but now I gain some progress.
I am stuck with the problem:
Input: list of consecutive primes i.e. '(2 3 5 7 11)
Output: list of their pairwise products without duplication '(4 6 10 14 22 9 15 21 33 25 35 55 49 77 121)
I can imagine imperative solution with do-list or so.
Recursive solution is also very simple:
Code: Select all
(defun p-mul (l)
"Simple recursive version
Input: List of different primes.
Returns: List of pairwise multiplied elements of list l with no duplicates"
(if (null (rest l))
(list (* (car l) (car l)))
(append (mapcar #'(lambda (x) (* x (first l)))
l)
(p-mul (cdr l)))
)
)
My last most successful solution is:
Code: Select all
(defun pair-mult (l)
"MAP___ version
Input: List of different primes.
Returns: List of pairwise multiplied elements of list l with no duplicates"
(maplist #'(lambda (sub)
(mapcar #'(lambda (y x) (* x y))
sub l))
l)
)
Which applied to the list '(2 3 5 7 11) gives: ((4 9 25 49 121) (6 15 35 77) (10 21 55) (14 33) (22)) an awfully nested answer.
I don't want to 'flatten' this answer, I am looking for an elegant map***'s expression.
Does it exist?
Adam.