3 ms·
This seems like such an obvious solution it makes me wonder why creators of Haskell did not think of it? Lists are a very basic type used all the time if you do
by galaxyLogic 8y ago
This seems like such an obvious solution it makes me wonder why creators of Haskell did not think of it? Lists are a very basic type used all the time if you don't get that right lot of the value of the great type-system is lost I would think.
- masklinn 8y agoI think it's because they weren't very clear on how they wanted to handle partial functions / error reporting, and historically functions like head are partial. Making them total makes them a bit heavier for the best case, and furthermore can seem unnecessary (as you could just pattern-match the list itself). You can also see this oddity in Haskell allowing non-exhaustive case, and allowing refutable patterns in bindings.
- lodi 8y agoThey did. The elm-style version is in the standard libraries as: listToMaybe :: [a] -> Maybe a (http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Maybe.html#v:listToMaybe http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-M...)
- galaxyLogic 8y agoThat's a lengthy name for the simple operation of getting the first element of a list if there is one. I would have preferred 'car' :-)
- lodi 8y agoIt's a bit verbose, but this kind of thing is far less prevalent in Haskell anyway; most of the time you destructure a list through pattern matching, not explicit head/tail functions.