Hacker News new | comments | show | ask | jobs | submit login

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.




> 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.


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.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: