Unofficial Hacker News client - receive Push Notifications on replies.

Roc version 0.1.0, eight years in the making (opens external site)

428 points by DrDeadCrash 11 hours ago | rtfeldman.com | 112 comments on HN | Vote on HN (opens Hacker News)

View full thread · View on Hacker News

Comment thread (5)

  1. >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 (1)
    1. 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.
      Replies (2)
      1. So, for the sake of pedantry, in the programming language world "multiple dispatch" (and "[dynamic] dispatch" in general) typically refers to late binding - the function that's called is determined at runtime, and the caller doesn't actually need to know about the implementation being called.

        What Haskell is doing there is basically just a kind of syntactic sugar around a pattern match, which is (to a very rough approximation) basically a super-powerful switch. Like a switch, the compress function knows about all of the branches statically, even if at runtime it dynamically selects which branch to take based on the structure of its arguments. And the structure of its arguments is "closed": it knows at compile-time all the different forms they can take.

        Haskell achieves dynamic dispatch through typeclasses, and it can do multiple dispatch with the MultiParamTypeClasses extension. With these a caller doesn't need to know about the implementation it's invoking, and it doesn't need to know in advance anything about the possible values of its arguments beyond that they have an implementation that can be used - typeclasses are open, you can always define new implementations without updating every caller that uses them.

        Replies (1)
        1. I agree with that, I tried to look for the correct term to describe this but the best thing I could find was multiple dispatch.

          It's not just Haskell, though. Like I said, Erlang/Elixer also support this, and it enables writing some really elegant code.

      2. I think you can do this in Clojure:

          (defn greet
            ([] "Hello!")
            ([name] (str "Hello, " name "!"))
            ([name title] (str "Hello, " title " " name "!")))
        
        Which one could argue is cleaner for multiple arguments, but you can't pattern match on arguments themselves this way (but you can using multimethod)