by hkolek · 13y ago · 0 comments
It is possible, for example, to go through some books, especially "The Joy Of Clojure" which contains 20 line of marketing slogans for 1 line of code, and make explicit commentaries on all the subtle differences, but I'm not going to perform such a tedious task for free.)
> "The Joy Of Clojure" which contains 20 line of marketing slogans for 1 line of code
> but I'm not going to perform such a tedious task for free
Well, obviously there's no point in discussing this further and we have to agree to disagree. Have a nice day anyway.
(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? (defn cross2 [xs ys]
(cond (or (and (sequential? xs) (empty? xs)) (empty? ys)) '()
(not (sequential? xs)) (cons (list xs (first ys)) (cross2 xs (rest ys)))
true (concat (cross2 (first xs) ys) (cross2 (rest xs) ys))))
which is pretty much exactly homologous, allowing for the detail that you can't ask if an atom is empty? in Clojure, cond (Arc-like) takes alternating conditions and consequents rather than condition-consequent pairs, and the spellings of the list operations no longer refer to IBM 709 machine instructions. Also, it works on any kind of sequences, not just lists, with of course a punishing performance overhead on sequences whose `rest` operation is slow.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:
(defn sc [x ys] ; scalar cross
(if (empty? ys) '()
(cons (list x (first ys)) (sc x (rest ys)))))
(defn cross [xs ys]
(if (or (empty? xs) (empty? ys)) '()
(concat (sc (first xs) ys) (cross (rest xs) ys))))
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:
(defn cross [xs ys]
(map (fn [x] (map (fn [y] (list x y)) ys)) xs))This definition seems to exclude Common Lisp from being a Lisp. That might be a defensible position, but it does call into question how your definition of "Lisp" is useful to you. It also seems to exclude languages like Dylan, which are commonly regarded as Lisps by people far cleverer than me.
> What is a lisp?
It's a very good question. I think PG sums it up quite nicely in "Revenge of the Nerds" [1]. Although, now that I'm thinking about it, I'm not quite sure there's any point in classifying something as a Lisp or not...
Again, Lisp could be defined as a limited set of conventions/features. As long as some other features, such as CLOS added there is no problem, but if some features are broken, then it is not Lisp anymore. It is just doesn't walk like a duck.
Let's say that Clojure was developed with a "put everything useful together" or Ruby-approach, if you wish, which is very popular for scripting languages, while development of Scheme and other Lisp dialects was founded on "put only what is absolutely essential, and done right".
The first approach "stuff anything in" you could see almost everywhere. The second approach "research first, and do the best" is unpopular for the obvious reasons and could be rarely seen only in masterpieces, such as Gambit-C, nginx, old-school marvels such as Informix.
So, in my opinion, Clojure is much closer to Ruby than to Lisp (let's not be deceived by parentheses) - it is a scripting language (to quickly put everything together with variety of clever special syntax and fancy data-structures without much thinking about implementation details). This is, of course, most productive approach to coding - this is why people love scripting languages so much.