Re:Functional programming explained in Clojure with a termin...
by sgeisenh on Thursday May 15, 2014 @03:13PM
(#7751540)
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.
[ Parent
| on HN ]
Re:Functional programming explained in Clojure with a termin...
by brudgers on Friday May 16, 2014 @09:56AM
(#7755493)
Yes, 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.
Re:Functional programming explained in Clojure with a termin...
by oskarkv on Friday May 16, 2014 @01:54AM
(#7753968)
No. 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.
Re:Functional programming explained in Clojure with a termin...
by ajuc on Friday May 16, 2014 @06:53AM
(#7754830)
Pattern 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.
Re:Functional programming explained in Clojure with a termin...
by malisper on Friday May 16, 2014 @09:45AM
(#7755435)
I 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).
Re:Functional programming explained in Clojure with a termin...
by LordQuas3000 on Thursday May 15, 2014 @03:17PM
(#7751572)
Nobody would actually implement an accumulator that way. He gave a super simplistic example to illustrate recursion.
Re:Functional programming explained in Clojure with a termin...
by technomancy on Thursday May 15, 2014 @07:42PM
(#7752954)
It'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.