It begins with head is a mistake! It should not be in the Prelude. Other partial Prelude functions you should almost never use include tail, init, last, and (!!). From this point on, using one of these functions on a homework assignment will lose style points!
And here is a proposal:
data NonEmptyList a = NEL a [a]
nelToList :: NonEmptyList a -> [a]
nelToList (NEL x xs) = x:xs
listToNel :: [a] -> Maybe (NonEmptyList a)
listToNel [] = Nothing
listToNel (x:xs) = Just $ NEL x xs
headNEL :: NonEmptyList a -> a
headNEL (NEL a _) = a
tailNEL :: NonEmptyList a -> [a]
tailNEL (NEL _ as) = as
Dear sirs, a List is a recursive data structure by definition. Non-recursive List is an unimaginable nonsense. What was made here with a name of safe(!) NonEmptyList is some ugly Pair with lots of syntactic noise.
How could you get the last element from your NonEmptyList? How could you tell how many elements in it?)
But, of course, never ever use these ugly unsafe lists - they might be empty! Use safe NonEmptyList because in Haskell everything is safe. And never try to think what the fuck you are reading.
NonEmptyList is usually implemented that way to be a bit lazier on the implementation end. You can also write it as
data NEL a = One a | More a (NEL a)
but then you have to write fresh functions for all of the default list methods without any performance improvements (which make doing so totally valuable for things like Vector). You also have NEL being an instance of a more general NonEmptyFunctor pattern like in (http://hackage.haskell.org/packages/archive/non-empty/0.1.3/...).
With this recursive NEL, last is
last :: NEL a -> a
last (One a) = a
last (More _ more) = last more
and (inefficient, corecursive) length is
len :: NEL a -> Int
len (One _) = 1
len (More _ more) = succ (len more)
NELs are quite useful not just for safety. head as type [a] -> a is largely considered to be a mistake, the Safe package is quite popular and redefines head as having type [a] -> Maybe a. I use it frequently.
NELs have properties like being Semigroups (and not Monoids) and Comonads which regular lists do not. This helps not only for theoretical guarantees of safety but also so that we can feel confident that our typeclass declarations have the meaning they're supposed to have.
So is (NEL a = NEL a [a]) a bad solution? It's perhaps less independently beautiful than the truly recursive NEL, but it's easier to write and takes advantage of Haskell's built in "possibly empty List" functionality. It's also more "globally beautiful" in that it fits into the family of types "non-empty functors". The recursive version is also trivially isomorphic, so there's no loss in functionality.
NEL was provided as an example to think about, not as a 'proposal'. There was no suggestion that students use this for everything.
Keep in mind that this is a structured 100-level class targeted at undergrads who are relatively new to programming and haven't seen much in the way of functional programming and Hindley-Milner typing.
The author and teacher of this course, Brent Yorgey, is one of those rare guys who is both a fantastic lecturer and a fantastic researcher.
For Penn's fuller version of this course, check out CIS 552[1]. Dr. Stephanie Weirich is also a fantastic lecturer, and a GHC contributor to boot (IIRC).
I started from LYAH and the only thing missing from it were some exercises so I could retain the information that was presented in each chapter. Otherwise, it's a fantastic book.
I really wanted to learn functional programming when my university offered it, but unfortunately it's crunched in a horribly busy term and I'm not willing to risk core modules for it if I end up struggling.
That said, I really do want to learn it and find MOOCs a really useful tool, if anybody has any suggestions for any they've used (or the one linked, I'm unclear if that's for students at that university only) that'd be awesome!
I find that this rather old video from OSCON 2007 by the incredible Simon Peyton Jones (of F# and GHC) is the most inspiring Haskell video there is. It uses the X window manager XMonad as the basis to explain many important concepts of Haskell and testing (QuickCheck). It is self contained and no previous knowledge of Haskell is needed:
I believe jumping right into Haskell, unless you're already fond of recursion, is a bit too stiff. Maybe try to read SICP, it will expose you to the functional programming principles that lead to Haskell (strict vs lazy evaluation) which he mostly makes more concise and expressive.
I'm currently trying to learn functional programming through Haskell. Glad to have found another resource, plus this cheat sheet linked from the page looks handy: http://cheatsheet.codeslower.com/
For my own 2-cents, once I learned QuickCheck[1] and Parsec[2], I found myself slowly rewriting them for every language I program in.
> Will it help me professionally?
Speaking as another professional, probably not.
I love Haskell, but my colleagues can't deal with it. They can't write it. They don't know functional programming. And to be entirely honest, they will never understand Monoids, Monads, or macro-hacking.
> ...Help me being a better programmer?
Maybe. If you like already like to think of software axiomatically, then yes, it will make you will make you think of software more conceptually.
But honestly, getting javascript, ruby and go under your belt will also make you think differently about software, and those are more practical languages.
[+] [-] dschiptsov|12 years ago|reply
It begins with head is a mistake! It should not be in the Prelude. Other partial Prelude functions you should almost never use include tail, init, last, and (!!). From this point on, using one of these functions on a homework assignment will lose style points!
And here is a proposal:
Dear sirs, a List is a recursive data structure by definition. Non-recursive List is an unimaginable nonsense. What was made here with a name of safe(!) NonEmptyList is some ugly Pair with lots of syntactic noise.How could you get the last element from your NonEmptyList? How could you tell how many elements in it?)
But, of course, never ever use these ugly unsafe lists - they might be empty! Use safe NonEmptyList because in Haskell everything is safe. And never try to think what the fuck you are reading.
[+] [-] Peaker|12 years ago|reply
That said, even NonEmptyList is sometimes useful. For example, some Prelude functions could use it:
Making common idioms like: Safe again.Not sure what you mean by:
> How could you get the last element from your NonEmptyList? How could you tell how many elements in it?) Exactly two.
[+] [-] tel|12 years ago|reply
With this recursive NEL, last is
and (inefficient, corecursive) length is NELs are quite useful not just for safety. head as type [a] -> a is largely considered to be a mistake, the Safe package is quite popular and redefines head as having type [a] -> Maybe a. I use it frequently.NELs have properties like being Semigroups (and not Monoids) and Comonads which regular lists do not. This helps not only for theoretical guarantees of safety but also so that we can feel confident that our typeclass declarations have the meaning they're supposed to have.
So is (NEL a = NEL a [a]) a bad solution? It's perhaps less independently beautiful than the truly recursive NEL, but it's easier to write and takes advantage of Haskell's built in "possibly empty List" functionality. It's also more "globally beautiful" in that it fits into the family of types "non-empty functors". The recursive version is also trivially isomorphic, so there's no loss in functionality.
[+] [-] lbolla|12 years ago|reply
But others have very good reasons to disagree: http://stackoverflow.com/questions/6364409/why-does-haskells...
[+] [-] LewisJEllis|12 years ago|reply
Keep in mind that this is a structured 100-level class targeted at undergrads who are relatively new to programming and haven't seen much in the way of functional programming and Hindley-Milner typing.
[+] [-] sold|12 years ago|reply
I don't think so: http://en.wikipedia.org/wiki/List_(abstract_data_type)
[+] [-] tome|12 years ago|reply
What's the justification for that assertion?
[+] [-] MaxScheiber|12 years ago|reply
For Penn's fuller version of this course, check out CIS 552[1]. Dr. Stephanie Weirich is also a fantastic lecturer, and a GHC contributor to boot (IIRC).
[1] http://www.seas.upenn.edu/~cis552/12fa/
[+] [-] Watabou|12 years ago|reply
I started from LYAH and the only thing missing from it were some exercises so I could retain the information that was presented in each chapter. Otherwise, it's a fantastic book.
[+] [-] mcintyre1994|12 years ago|reply
That said, I really do want to learn it and find MOOCs a really useful tool, if anybody has any suggestions for any they've used (or the one linked, I'm unclear if that's for students at that university only) that'd be awesome!
[+] [-] archarios|12 years ago|reply
[+] [-] ilolu|12 years ago|reply
[+] [-] ekm2|12 years ago|reply
[1]http://learnyouahaskell.com/
[2]http://book.realworldhaskell.org/
[+] [-] bestham|12 years ago|reply
http://blip.tv/oreilly-open-source-convention/oscon-2007-sim...
[+] [-] agumonkey|12 years ago|reply
Here's a playlist of a lecture session for HP employees by the authors of the book : http://www.youtube.com/playlist?list=PLB63C06FAF154F047
Here's the book http://mitpress.mit.edu/sicp/ ( alternate formatting texinfo http://www.neilvandyke.org/sicp-texi/ , pdf http://sicpebook.wordpress.com/ )
[+] [-] ludwigvan|12 years ago|reply
http://channel9.msdn.com/Series/C9-Lectures-Erik-Meijer-Func...
[+] [-] magmadiver|12 years ago|reply
also this http://lisperati.com/haskell/
[+] [-] croyd|12 years ago|reply
[+] [-] JacobHenner|12 years ago|reply
[+] [-] WasimBhai|12 years ago|reply
[+] [-] shoki|12 years ago|reply
http://stackoverflow.com/questions/1604790/what-is-haskell-a...
For my own 2-cents, once I learned QuickCheck[1] and Parsec[2], I found myself slowly rewriting them for every language I program in.
> Will it help me professionally?
Speaking as another professional, probably not.
I love Haskell, but my colleagues can't deal with it. They can't write it. They don't know functional programming. And to be entirely honest, they will never understand Monoids, Monads, or macro-hacking.
> ...Help me being a better programmer?
Maybe. If you like already like to think of software axiomatically, then yes, it will make you will make you think of software more conceptually.
But honestly, getting javascript, ruby and go under your belt will also make you think differently about software, and those are more practical languages.
[1] http://en.wikipedia.org/wiki/QuickCheck [2] http://www.haskell.org/haskellwiki/Parsec
[+] [-] Peaker|12 years ago|reply
[+] [-] gtani|12 years ago|reply
http://failex.blogspot.com/
http://www.haskell.org/haskellwiki/Introduction
http://fvisser.nl/post/2013/april/23/slide-cfams-talk.html
https://news.ycombinator.com/item?id=5688803
[+] [-] dons|12 years ago|reply
[+] [-] Dewie|12 years ago|reply
[+] [-] xzel|12 years ago|reply
[+] [-] Ashuu|12 years ago|reply