3 ms·
Does anyone else find Clojure's syntax a bit obtuse? I feel like basic recursive definitions are much clearer in Haskell and ML. To sum over a list using an acc
by sgeisenh 12y ago
Does anyone else find Clojure's syntax a bit obtuse? I feel like basic recursive definitions are much clearer in Haskell and ML. To sum over a list using an accumulator shouldn't take more than two or three trivial lines of code.
- LordQuas3000 12y agoNobody would actually implement an accumulator that way. He gave a super simplistic example to illustrate recursion.
- technomancy 12y agoIt's definitely unfortunate that Clojure doesn't ship with pattern matching out of the box, but there's a library that implements pattern matching macros. But obviously summing over a list can be done in one line of code in any decent language even without pattern matching.
- oskarkv 12y agoNo. I love it. It's so simple. Even if Haskell has pretty nice syntax, I found myself resenting it because I needed to remember operator precedence.
- ajuc 12y agoPattern matching would be nice, but destructuring helps. (defn sum-acc ([xs] (sum-acc xs 0)) ([[x & xs] acc] (if (nil? x) acc (recur xs (+ x acc))))) Could you show Haskell equivalent? BTW there should be a macro that adds argument with default value.
- malisper 12y agoI believe it should be something along the lines of: sumacc xs = summer xs 0 where summer [] n = n summer (x:xs) n = summer xs (x+n) I'm sure there are macros in some library somewhere that implement pattern matching in lisp (regardless of dialect).
- brudgers 12y agoYes, and no. Viewed as a Lisp, it trades some simplicity for practicality, e.g. vectors rather than lists as the container for arguments to a function. Neither than `lambda` template for Common Lisp's `defun` macro or the "usage" template for Scheme's `define` are consistent with the overall language syntax because the list should be quoted or executed. On the other hand, viewed as a DSL for the JVM implemented as a Java library or as an evolutionary step in Lisp, Clojure's syntax makes a lot more sense. Summing over a collection is a one liner in Clojure (as in many other languages): => (reduce + (range 10)) 45 Clojure's dynamic typing trades simplicity of syntax for the static typing safety harness required in ML or Haskell.