2 ms·
That is one of the most enlightening criticisms I have ever read. Thank you. I'll try and fix my post tonight. Now, about your questions… float foo(int);
by loup-vaillant 14y ago
That is one of the most enlightening criticisms I have ever read. Thank you. I'll try and fix my post tonight.
Now, about your questions…
float foo(int);
translates to
foo :: Int -> Float
Now there's still a slight difference: in C, the prototype can be used as a forward declaration. The Haskell equivalent is merely a type restriction. Here it says "dear compiler, whatever you may think about that 'foo' thing, I'm telling you this is a function from integers to floating points.
Oh, and type names are capitalized in Haskell. "list" was a typo.
Now,
(.) :: (b -> c) -> (a -> b) -> (a -> c)
reads in a similar way. On the left side of '::', we have an identifier (which here happens to be an infix operator, hence the surrounding parentheses). On the right side of '::', we have the type we wish to declare (which here is a bit complicated).
g . f = λx -> g (f x)
I'm just defining a new value here. The left side of '=' tells us we define the dot operator (which is a function with two arguments), and the right hand side is just the body of the function.
λx -> blah_blah
is a functional value. 'λ' and '->' are the syntax constructs. 'x' is the parameter, and 'blah_blah is the body. This code reads "the function which produces 'blah_blah' from 'x'". I call this a "literal" function, or a functional literal, because it is analogous to integer literals
42
and string literals:
"Hello World"
(Oh and in Haskell, we actually use '\', not 'λ'.)
A cons cell is just a place where you store two values. Here this particular cons cell contains an element, and a pointer to the rest of the list. I reckon I was going way too fast on that one.
In type declarations, the arrow is right associative. So,
foo :: a -> b -> c
is the same as
foo :: a -> (b -> c)
You can think of it as a functions with two arguments, or as a function with one arguments, which returns a function with the remaining argument.
map (a -> b) -> List a -> list b
Lacks the '::' because I forgot to write it. Good catch. Likewise 'dbl_all' is a typo, it should be 'square_all' or something.