logoalt Hacker News

masijo • yesterday at 2:31 PM • 1 reply • view on HN

>missed it in languages like Clojure

I'm not sure about Haskell and Erlang/Elixir since I haven't used them much, but aren't multimethods the Clojure solution for this?


Replies

stingraycharles • yesterday at 3:31 PM

Not really, what Haskell / Erlang offer is that you can do pattern matching in the function signature.

So you can, say, have a definition for when the value of n is 0, n is 1, etc.

This is a nice example in Haskell:

   compress :: Eq a => [a] -> [a]
   compress [] = []
   compress [x] = [x]
   compress (x:ys@(y:_))
     | x == y    = compress ys
     | otherwise = x : compress ys
Basically it enables you to use recursion more elegantly because the pattern matching is folded into the function signature.
➕ show 3 replies