Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I love the tutorial! I couldn't help writing down some notes while going through it, here they are if you're interested in reading:

- Really would love to return to previous steps to review things (for example, reviewing what the difference is between a list and a tuple)

- "You just passed the (+1) function to the map function." But didn't I also pass [1..5] to it too? It's unclear to me whether map is a function which takes two arguments ... or if the first part "map (+1)" evaluates to a function which acts on [1..5]

- Is a (+1) a tuple with one item (the function "+1") or is it just similar syntax?

- Really like the exercises and would love to have more challenges interspersing the intake of new content. It honestly feels like the best way to learn.

I'm going to sleep for the night, otherwise I'd keep going. I'll return to it later. Looks promising!

P.S. Mini-bug report: Somehow this happened and it really confused me https://i.cloudup.com/0wRJS7FZls.png - looks like my 'try it again anyways' strategy saved me here. (there were no js console errors).



Not sure if you would like to know some answers to your questions, here are they anyway:

- It's unclear to me whether map is a function which takes two arguments ... or if the first part "map (+1)" evaluates to a function which acts on [1..5]

The latter is true. In Haskell you can pass arguments to functions one at a time each time the result could be a function that takes another argument. You might even say that Haskell functions always take one argument, and that Haskell's function syntax is just a bit of sugaring. The process of returning a function that takes the second argument is called a 'curry' after the man Haskell Curry.

- Is a (+1) a tuple with one item (the function "+1") or is it just similar syntax?

A tuple of 1 is just an expression. The ('s are just to determine expression scope, it's the commas that would make it a tuple. A tuple of 2 would just be 2 expressions bundled together, so I don't think there's a meaningful difference :P


- You might even say that Haskell functions always take one argument

This is exactly true. Every Haskell function takes either zero arguments, or one.


Nah, Haskell has no functions with zero arguments.

You could say that a value is a function that takes no arguments, but then you lose the distinction between functions and other values, and more importantly, the distinction between function types and other types! For example, types like Bool have decidable equality, but function types in general have undecidable equality, so you really don't want to be caught saying that Bool is a function type. It's much more sensible to say a type is a function type iff it was constructed by the type constructor ->, which takes two types, the argument type and the return type. In other words, all functions take one argument and return one result.

You could also say that a value is an unevaluated thunk that takes no arguments, but that's not always true. The thunk may be already evaluated and replaced with a value, without affecting the visible type. It's better to keep the idea of "thunk" separate from the idea of "function", because a value could be both, either, or none.

You could also say that every value x can be converted to and from a function () -> x. But that's not a function with no arguments, that's a function with one argument of type "unit" (the type with a single member, a.k.a. the empty tuple).

The above might sound pedantic, but for some reason it's fascinating to me to think about such things. Language design is a kind of addiction and Haskell is like a drug, because its original purpose was to be a laboratory for programming language research, which you can see in the million GHC extensions.


Haskell functions never take zero arguments. Things that take zero arguments are not functions. For a more in-depth explanation of why, see: http://conal.net/blog/posts/everything-is-a-function-in-hask...


As said by thinkpad in this thread somewhere, Haskell functions are unary.


'(+1)' is a function expressed in 'section form'. It is equivalent to the function '\x -> x + 1'.


In Haskell, all functions take one argument. Also, operators are functions.

First, the one argument thing is known as currying. With higher-order functions, returning a function is admissible, so

    map :: (a -> b) -> [a] -> [b]
can also be thought of as:

    map :: (a -> b) -> ([a] -> [b])
like so:

    ghci> :t map length
    map length :: [[a]] -> [Int]
As for operators being functions, function names that are alphanumeric default to prefix (e.g. f 10) while those that are symbolic default to infix (e.g. 10 + 18). To use an alphanumeric name in infix, use backticks, e.g.:

    ghci> 5 `max` 6
    6
Use parentheses to prefix an "operator"-style name, like so:

    ghci> (+) 5 6
    11
The (+1) that you're meeting is an additional bit of syntactic sugar. It's identical to (\x -> x + 1). It allows you to do things like this:

    ghci> map (2^) [1..5]
    [2,4,8,16,32]
    ghci> map (^2) [1..5]
    [1,4,9,16,25]




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: