4 ms·
compare this proposal to the pattern matching you get from Haskell or OCaml and weep. for example (OCaml): let imply = function | (true,false) ->
by toolslive 7y ago
compare this proposal to the pattern matching you get from Haskell or OCaml and weep.
for example (OCaml):
let imply = function
| (true,false) -> false
| _ -> true
- macintux 7y agoPattern matching across multiple function heads (ML, Erlang) is incredibly liberating. I hear Haskell has it but it's not idiomatic, and I think Elm doesn't support it at all. Frustrating. I've found half-hearted pattern matching solutions like Scala quite disappointing.
- lalaithion 7y agoCan you give an example of the pattern matching in ML and Erlang that is not idiomatic in Haskell?
- macintux 7y agoIt's the multiple function heads which (again, I've been informed, no direct knowledge) is discouraged. e.g. https://github.com/macintux/advent-of-code-2019/blob/master/day4/day4.erl#L34-L41 https://github.com/macintux/advent-of-code-2019/blob/master/...
- p0llard 7y agoYou mean a piecewise definition of a function? I don't think this is unidiomatic or discouraged in Haskell at all.
- macintux 7y agoThere's some indirect discussion here that seems to contradict that, but I certainly could be wrong. https://github.com/elm/compiler/issues/973 https://github.com/elm/compiler/issues/973
- Iceland_jack 7y agoThat's in the context of Elm, Evan gives his reasons but in the context of Haskell such multi-line equations are very common and normal Haskell style.
- a1369209993 7y agoTranslating that into Haskell, I get: check_each :: Int -> [Int] -> Bool -> Int -> (Bool,Bool,Int) check_each x (y:_) _ last | y==x && x==last = (True,False,x) check_each x (y:_) _ last | y>x && x==last = (True,True,x) check_each x (y:_) found _ | y>x = (True,found,x) check_each x _ found _ = (False,found,x) is_valid :: (Bool,Bool,Int) -> [Int] -> Bool is_valid (False,_,_) _ = False is_valid (True,True,-1) [_] = False is_valid (True,True,_) [_] = True is_valid (True,found,shortDup) (h:t) = is_valid (check_each h t found shortDup) t which is ugly, certainly, but compiles fine (ghc). The uglyness mainly comes from the data structures, and I don't know enough about the domain to suggest better ones (there might not be better ones).
- macintux 7y agoWell, it was code I threw together quickly for Advent of Code, so try not to be too hard on it.
- a1369209993 7y agoEh, like I said, that might be the best you can do for a messy problem domain. I'm still not sure what you meant by multiple function heads though.
- nv-vn 7y agoOCaml, F#, ReasonML don't support that, I think it's unique to SML in the ML family.
- macintux 7y agoInteresting, thanks. SML is my only direct exposure to the family, and I recall a conference speaker mentioning that ML (perhaps SML) programmers weren't fond of the feature.