3 ms·
That article was more about declarative/functional programming than about the details of the Haskell syntax. Types are capitalized. Lower-case types in signatu
by latk 14y ago
That article was more about declarative/functional programming than about the details of the Haskell syntax.
Types are capitalized. Lower-case types in signatures are placeholders in polymorphic functions. The signature
(.) :: (b -> c) -> (a -> b) -> (a -> c)
means: The function that is the . takes a first argument (g) that maps some type b to some type c. The second argument (f) maps some type a to the type b. This ensures that you can always do g(f(x)). The result of the function . is a function that maps the type a (what f takes) to type c (what g returns).
The type signature only deals with types; this is not connected to the names of the function parameters.
Lambda expressions are written without types; they can be inferred (i.e. the return type is not void* , but exactly determined by the compiler). A lambda expression in lambda calculus notation would be written "λx.(+ x 1)". Haskell slightly modifies this, and allows infix operators: "\x -> x + 1"(the \ looks a bit like λ). Punctuation functions are infix by default.
About the data expression: This constructs a new type (typedef). The "|" operator is just used in a declarative fashion. Every time you write Empty in your source code, that thing is a list. The expression "list = Cons foo Empty" would create a list of one element (foo). A cons cell is a pair, this terminology is popular in Lisp. These confusing data type constructors allow pattern matching, as you have seen in the definition of "map": If the list is Empty, return Empty. If the list is a Cons of an element and another list, then return a Cons of (result of our function applied to that element) and (the rest of the list mapped) – recursion.