4 ms·
Can you give an example of the pattern matching in ML and Erlang that is not idiomatic in Haskell?
by lalaithion 7y ago
Can 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.