Live data from Hacker News

Learn You a Haskell for Great Good

learnyouahaskell.com

31–40 of 67 posts

Re: Learn You a Haskell for Great Good

#31

What I find confusing with Haskell is that the keywords don't seem to be consistent with the concepts they're meant to express. As someone new to Haskell I learnt that the `class` keyword doesn't express the object-oriented concept of class, but the concept of type class, which is different. Fair enough. What about declaring types then? That must be `type`, right? Nope, `type` is for creating type aliases. OK, surely…

The designers of Haskell (and Haskell is one of the few successful design-by-committee languages,) were not doing things "your way" (where I assume you are a working programmer, with knowledge of some or even many traditional imperative languages.)

They were PL researchers. Used to ML, Common Lisp, Ada, and, most importantly, Mathematics and Category and Type Theory.

`data` introduces an algebraic data type, so it makes sense to use data as a keyword. `type` and `newtype`, on the other hand, are programmer's conveniences: both carry no runtime cost, they are not actually part of the semantics of the backend language.

`map` is a function that's really just a specialization of `fmap`. Now, my category theory is a bit weak, but if I'm thinking correctly here, `fmap f` is an endofunctor in the category of functors, and so it makes perfect sense to call "mappable" Functor. There's also "Traversable," which works pretty much like a fully-fledged "mappable" with all extras, but also requires a Functor instance.

Keywords, and idiosyncrasies of a syntactic nature are really just superficial. The actual elegance of the language is its type system. (The actual warts of the language are, for the most part, also in the type system; records for example.) The latter criticism about `mappable` is really just due to the fact that you haven't gotten used to the underlying theory (and I don't blame you, it's hard.) But getting used to it can be rewarding. I'm not saying it's going to make you a better programmer (I'm not necessarily of that opinion,) but it's rewarding in its own right.

Oh, and the difference between data constructors, type names, and, later, kind names (with promotion, since 7.4,) can be confusing. I sometimes find it an oversight that they are not distinguished syntactically, I think that would greatly help most newbies, and sometimes even make things easier to read and understand for veterans, too.

Re: Learn You a Haskell for Great Good

#32
post #7

Is the name of this book some sort of inside joke or something?

It is a joke, but not an inside joke as much as a joke that would be understood by people from Balkan countries (Serbia, Croatia, Bosnia). Miran (the author of LYAH) is, judging from his first and last name, from this region. In the local language (Serbian, Croatian and Bosnian are basically the dialects of the same common language), the most commonly used word for teach and learn is the same word, uciti, although there are separate words for these activities. Basically, whether you speak of teaching someone, or learning something is judged by the context. Then, Learn you a haskell vs. teach you haskell is a wordplay in that sense. A similar joke from the region: "Can you translate me to the other page of the street?"

Re: Learn You a Haskell for Great Good

#33
post #7

Is the name of this book some sort of inside joke or something?

It is a joke, but not an inside joke as much as a joke that would be understood by people from Balkan countries (Serbia, Croatia, Bosnia). Miran (the author of LYAH) is, judging from his first and last name, from this region. In the local language (Serbian, Croatian and Bosnian are basically the dialects of the same common language), the most commonly used word for teach and learn is the same word, uciti, although th…

Of course the strangest part of the title is the article "a" since "learn you" is a valid if unusual idiom in English.

Re: Learn You a Haskell for Great Good

#34
post #17
post #5

Haskell looks very interesting but I can't help but ask 'why do I need it?'. Currently I am learning Java since I want to get a jr.developer job later on and Ruby for some scripting and personal projects(maybe even Rails later). So Java is used in industry, Ruby is used in web and metasploit/ronin (something that I like to mess with), but what about Haskell? I am genuinely curious what Haskell can offer. I always see…

> I always see it mentioned on Reddit but I have yet to come across any notable project written with it. xmonad, one of the best tiling window managers for Linux, is written in Haskell (and is open source).

Not sure if it's notable, but I like hpodder, a command-line podcast client. It happens to be written by one of the author of RWH.

Re: Learn You a Haskell for Great Good

#35
I didn't like LYaHfGG.

Take this in p52 for instance:

maximum' :: (Ord a) => [a] -> a

maximum' [] = error "Maximum of empty list!"

maximum' [x] = x

maximum' (x:xs) = max x (maximum xs)

Now, you may think that implies that you could do something like this to print a string one character at a time:

rsoat (x:xs) = show x (rsoat xs)

Buy you can't. If you try that Haskell complains "The function 'show' is applied to two arguments, but its type 'a0 -> String' has only one...."

Fair enough, one might reason - you're just passing max two things when it wants one so we can do something like this:

rsoat [] = []

rsoat [x] = show x

rsoat (x:xs) = rsoat xs

But no, that just gets you the tail of your string because x:xs doesn't match x so it never prints the other bits.

What you may end up doing is something like this:

rsoat [] = []

rsoat [x] = [x]

rsoat (x:xs) = show x ++ (rsoat xs)

But that's still not really doing what you wanted to do in the first place. It's sticking all your characters into a string and then reading that string all at once. And it's not a particularly obvious solution either if you don't know the language.

This may seem a silly example. But what happens when, as is frequently the case, someone wants to retrieve the nth element of a list? (list !! n) Haskell, as learnt through Learn You a Haskell, can easily be expected to drive someone up the wall who starts asking such questions - it's not a whole bunch of fun to play with.

Re: Learn You a Haskell for Great Good

#36

I didn't like LYaHfGG. Take this in p52 for instance: maximum' :: (Ord a) => [a] -> a maximum' [] = error "Maximum of empty list!" maximum' [x] = x maximum' (x:xs) = max x (maximum xs) Now, you may think that implies that you could do something like this to print a string one character at a time: rsoat (x:xs) = show x (rsoat xs) Buy you can't. If you try that Haskell complains "The function 'show' is applied to two a…

To be honest, it does not seem you did a very good job of reading the book. If you're rooted in imperative languages, it takes some effort. I read the book when I had already had some basic functional programming experience, so my experience might not be so representative, but I've heard quite a few positive reviews from people for whom it was the first introduction to FP.

You seem to start off from a wrong premise that the show function prints things. It doesn't - it simply converts whatever value (of type a) you give it to a String (provided a is an instance of the Show typeclass). So seeing that you already have a string, a function which converts things to strings will probably not be very useful.

> rsoat [] = [] > rsoat [x] = show x > rsoat (x:xs) = rsoat xs

> But no, that just gets you the tail of your string because x:xs doesn't match x so it never prints the other bits.

Uh, no. It doesn't give you the tail of the string, it gives you a list containing just the last element of the argument (if the argument's not empty), or the empty list (if the argument is empty) - which you can read directly from your definition.

Furthermore, " x:xs doesn't match x" doesn't make a whole lot of sense - it's not supposed to match x, it's supposed to match a non-empty list. Which it does, and the first element of such a list gets bound to the parameter x - but you do not use the parameter in your right-hand side of the definition, so you effectively discard it.

Note that both pattern matching and the Show typeclass (and the corresponding function) are discussed in the book prior to the minimum example.

Re: Learn You a Haskell for Great Good

#37
post #36

I didn't like LYaHfGG. Take this in p52 for instance: maximum' :: (Ord a) => [a] -> a maximum' [] = error "Maximum of empty list!" maximum' [x] = x maximum' (x:xs) = max x (maximum xs) Now, you may think that implies that you could do something like this to print a string one character at a time: rsoat (x:xs) = show x (rsoat xs) Buy you can't. If you try that Haskell complains "The function 'show' is applied to two a…

To be honest, it does not seem you did a very good job of reading the book. If you're rooted in imperative languages, it takes some effort. I read the book when I had already had some basic functional programming experience, so my experience might not be so representative, but I've heard quite a few positive reviews from people for whom it was the first introduction to FP. You seem to start off from a wrong premise t…

> You seem to start off from a wrong premise that the show function prints things.

No, I know what it does. That's how the book describes it however:

"The most commonly used function that operate on instances of this type class is show, _which prints the given value as a string_"

> Uh, no. It doesn't give you the tail of the string, it gives you a list containing just the last element of the argument (if the argument's not empty), or the empty list (if the argument is empty) - which you can read directly from your definition.

I continually forget that tail means the rest of the thing.

> Furthermore, " x:xs doesn't match x" doesn't make a whole lot of sense - it's not supposed to match x, it's supposed to match a non-empty list. Which it does, and the first element of such a list gets bound to the parameter x - but you do not use the parameter in your right-hand side of the definition, so you effectively discard it.

We're far enough away from what each other are thinking that this just isn't worth talking about as written.

You've already matched your list to x earlier in the program so x:xs doesn't get a chance to run. You're not showing x and then falling through to pass xs to the function again and chopping the first element off of that to show - and so on.

> Note that both pattern matching and the Show typeclass (and the corresponding function) are discussed in the book prior to the minimum example.

This:

http://i.imgur.com/7NKReoL.jpg

is not a discussion of the show type class and corresponding function.

Re: Learn You a Haskell for Great Good

#38

I didn't like LYaHfGG. Take this in p52 for instance: maximum' :: (Ord a) => [a] -> a maximum' [] = error "Maximum of empty list!" maximum' [x] = x maximum' (x:xs) = max x (maximum xs) Now, you may think that implies that you could do something like this to print a string one character at a time: rsoat (x:xs) = show x (rsoat xs) Buy you can't. If you try that Haskell complains "The function 'show' is applied to two a…

>Now, you may think that implies that you could do something like this to print a string one character at a time:

I'm not sure how far you got after this point, but the problem is easily solved with a higher-order function:

    mapM_ print "blahblahblah"
Or if you don't like each character being quoted:

    mapM_ (putStrLn . return) "hello one at a time"
Or did you not want the characters on their own lines? In that case, you can do this:

    mapM_ putChar "horse raddish"
But why bother doing that when you can simply do this?:

    putStr "egg freckles"
>But what happens when, as is frequently the case, someone wants to retrieve the nth element of a list?

If you are frequently wanting to do this, you are probably doing something wrong. Haskell's lists are intended to be used as control structures, not indexed data structures. For those, you'll want an Array, Vector, IntMap etc.

Re: Learn You a Haskell for Great Good

#39

What I find confusing with Haskell is that the keywords don't seem to be consistent with the concepts they're meant to express. As someone new to Haskell I learnt that the `class` keyword doesn't express the object-oriented concept of class, but the concept of type class, which is different. Fair enough. What about declaring types then? That must be `type`, right? Nope, `type` is for creating type aliases. OK, surely…

The designers of Haskell (and Haskell is one of the few successful design-by-committee languages,) were not doing things "your way" (where I assume you are a working programmer, with knowledge of some or even many traditional imperative languages.) They were PL researchers. Used to ML, Common Lisp, Ada, and, most importantly, Mathematics and Category and Type Theory. `data` introduces an algebraic data type , so it m…

+1 for your thoughtful reply.

I've tinkered with other functional languages (Erlang, Scheme and Clojure), but Haskell is a stranger beast.

Re: Learn You a Haskell for Great Good

#40
post #36

Earlier quoted context omitted.

To be honest, it does not seem you did a very good job of reading the book. If you're rooted in imperative languages, it takes some effort. I read the book when I had already had some basic functional programming experience, so my experience might not be so representative, but I've heard quite a few positive reviews from people for whom it was the first introduction to FP. You seem to start off from a wrong premise t…

> You seem to start off from a wrong premise that the show function prints things. No, I know what it does. That's how the book describes it however: "The most commonly used function that operate on instances of this type class is show, _which prints the given value as a string_ " > Uh, no. It doesn't give you the tail of the string, it gives you a list containing just the last element of the argument (if the argumen…

Fair enough, I don't have the printed version around. It's wrong on that point and I can see that it can be misleading. To their credit, here's the current formulation from the online version:

> Members of Show can be presented as strings. All types covered so far except for functions are a part of Show. The most used function that deals with the Show typeclass is show. It takes a value whose type is a member of Show and presents it to us as a string.

This is correct. Whether it qualifies as a proper discussion might be debatable, but since the particular typeclass is simple enough, and seeing that this appears very early in the book, it seems reasonable.

> You've already matched your list to x earlier in the program so x:xs doesn't get a chance to run. You're not showing x and then falling through to pass xs to the function again and chopping the first element off of that to show - and so on.

This is incorrect. Definitions are not really "executed" (or run) in Haskell; you simply replace the left-hand side of the definition with the right-hand side. You use the first definition (in file order) with a matching LHS. So in your example, the third definition for rsoat would be used for all lists with more than one element. So the "falling-through" as meant here is not what you get with, say, a switch statement in C.

Post reply on HN