|
|
|
|
|
by dschiptsov
4714 days ago
|
|
Let's make it simple.) There is classic homework code in two different dialects of Lisp: (define (cross xs ys)
(cond ((or (null? xs) (null? ys)) '())
((atom? xs) (cons (list xs (car ys)) (cross xs (cdr ys))))
(else (append (cross (car xs) ys) (cross (cdr xs) ys)))))
(defun cross (xs ys)
(cond ((or (null xs) (null ys)) nil)
((atom xs) (cons (list xs (car ys)) (cross xs (cdr ys))))
(t (append (cross (car xs) ys) (cross (cdr xs) ys)))))
Could you, please, provide the equivalent code in Clojure? |
|
But I would argue that this interface is poorly designed, since you can say (cross2 '(a b c) '(1 2 3)) or (cross2 'a '(1 2 3)) but not (cross2 '(a b c) '1), and worse, (cross2 '(a (b c) d) '(1 2 3)) implicitly flattens the (b c) into individual items, which is probably a latent bug rather than desired behavior. So I would argue for writing it in this form instead:
which avoids those irregularities and makes the code easier to understand by removing misleading false symmetries.Except really, if this isn't a homework problem, I think you should write it like this in any of these three Lisps: