News for hackers. Stuff that matters.
Re:Elm 0.15: Asynchrony with Tasks by taylorfausak on Monday April 20, 2015 @06:56PM (#9411340)
The decisions the Elm team make continue to impress me. I like that they are willing to break from tradition in order to create a better programming environment. In particular, changing the head function to be total is a great move.

    head : List a -> Maybe a
Compare that with the same function in Haskell.

    head :: [a] -> a
To me, the Elm version makes more sense. (It also avoids having to explain that "[a]" means "list of 'a's".)
Re:Elm 0.15: Asynchrony with Tasks by lisperforlife on Monday April 20, 2015 @07:31PM (#9411457)
In addition to the lexical change, the result now returns a Maybe instead of the actual element. This was done to avoid runtime crashes when head encounters an empty list. However, this means that everytime you have to use head, you will have to pattern match on Just x/Nothing to consume it. However, you can still use the (x::xs) pattern matching syntax to obtain head and tail and pattern match on [] for empty lists.

Absence of applicatives or monads mean that you have to explicitly pattern match on Just/Nothing everytime. There is a proposal to add `?` as an operator to obtain the head of the list without the Maybe and provide a default value if it is Nothing. Also there is another proposal to allow `unsafe` for the program to crash if the result is Nothing.

Re:Elm 0.15: Asynchrony with Tasks by dragonwriter on Monday April 20, 2015 @08:11PM (#9411604)
> Absence of applicatives or monads mean that you have to explicitly pattern match on Just/Nothing everytime.

It seems to me that most real uses can probably be solved by map, and withDefault if you really need to get out of the Maybe monad for some reason (though map should make it so that you don't have to for most things.)

> There is a proposal to add `?` as an operator to obtain the head of the list without the Maybe and provide a default value if it is Nothing.

Since elm supports user-defined operators, can't you just do that yourself. Something like:

  list ? default = withDefault default (head list)
Re:Elm 0.15: Asynchrony with Tasks by rtfeldman on Monday April 20, 2015 @08:13PM (#9411613)
Yep! The proposal is to add that to the core libraries, and is basically just waiting to see how much people actually want to use withDefault that way in practice.
Re:Elm 0.15: Asynchrony with Tasks by rtfeldman on Monday April 20, 2015 @08:12PM (#9411607)
There's already withDefault to make that concise, so you can do things like this:

  withDefault 0 (head listOfNumbers)
No need to pattern match every time. :)

Docs for withDefault: http://package.elm-lang.org/packages/elm-lang/core/1.0.0/May...

Re:Elm 0.15: Asynchrony with Tasks by RehnoLindeque on Tuesday April 21, 2015 @06:37AM (#9413155)
Re:Elm 0.15: Asynchrony with Tasks by munro on Monday April 20, 2015 @07:38PM (#9411480)
head and (!!) have been a source of off by one errors for me. I've yet to learn Idris, but I have the hope that dependent types may save me from off by one errors. Sometimes you always want head to return something, so it would be nice to find those logic errors statically.

In this case, I definitely prefer the correct version Elm implements. I think Haskell just doesn't want to admit it's type system can't model everything. :)

Re:Elm 0.15: Asynchrony with Tasks by pka on Tuesday April 21, 2015 @12:55AM (#9412355)
It would probably help to also have something like this:

  singleton : a -> NonEmpty a
  cons : a -> NonEmpty a -> NonEmpty a
  head : NonEmpty a -> a
  last : NonEmpty a -> a
  init : NonEmpty a -> Maybe (NonEmpty a)
  tail : NonEmpty a -> Maybe (NonEmpty a)