3 ms·
First, if you are interested in Haskell, check out Learn You a Haskell For Great Good (http://learnyouahaskell.com/ http://learnyouahaskell.com/). I can not rec
by polymatter 14y ago
First, if you are interested in Haskell, check out Learn You a Haskell For Great Good (http://learnyouahaskell.com/ http://learnyouahaskell.com/). I can not recommend it highly enough for actually understanding Haskell. I will try to answer your queries, but I think you may understand it better if you just flick through the chapter on Higher Order Functions.
> Would be great if you offered an example where the argument and return type differed. As is, I don't know which order the Haskell declaration uses.
In Haskell, the last (rightmost) type is the return type. eg. countLetters :: String -> Int means it takes an argument of type String and returns an argument of type Int. Also, types are chained together so for example, foobar :: String -> Int -> Bool is a function that takes two arguments, the first being a String, the second an Int and it returns a Bool and foobarbaz :: String -> Int -> Bool -> Char is a function that takes three arguments, the first a String, the second an Int, the third a Bool and it returns a Char. This syntax makes a lot of sense when exploiting "higher-order functions" and "currying". I mention this only to provide keywords for your personal search.
> If this is essential at this point, it should be fleshed out more. Otherwise it feels like a distracting aside and out of place in a short introduction.
In my view this is an aside.
> Not clear why the left side is (.). Is this because it's non-alpha? Because it's infix? Something else? Order to read right hand side still unclear. Also don't know if reuse of a, b, and c as variables is significant.
To write a function whose name starts with a punctuation character, Haskell syntax demands that it is wrapped by brackets and I think its automatically infix. You can use any function infix by wrapping it in ` eg. x `f` y is the same as f x y which means (f x) y which means apply the function f to the argument x, and then to the argument y.
'a', 'b' and 'c' are type variables, similar to type variables in Java and C# when dealing with generics. They can stand for any type. So, 'a' could be String or Char or Bool or any other type. As could 'b' and 'c'. So 'a' may or may not be the same type as 'b' or 'c'.
I'm not sure I can make it any clearer but note the signature of (.) has two arguments and a single return type. The first argument is (b -> c) which means it is a function that takes a single argument 'b' and returns a value of type 'c'. So the first argument of (.) is a function. The second argument is (a -> b) which is also a function. Its return type is also a function.
Personally I think (.) is a very hard function to grok and certainly far too complex for the first higher order function you see, exponentially so if you're still trying to understand Haskell syntax.
> Would help to define a "cons cell". Is Cons a data type or a function here? Is there a standard for capitalization? Not clear to me how the 'a' on the left side relates to the two on the right. Is 'data' a keyword, a type, or something else?
data is a keyword. Its how you define a data structure in Haskell. In this case, its defining a data structure called List which will wrap elements of type 'a'. In other words, 'List Int' is a type, 'List Bool' is a type, 'List Char' is a type. List is broadly the same as generic in Java or C# where List<String> is a type and List<Bool> is a different type. Cons is a data constructor (and yes, its standard to capitalise data contructors), which you can think of as being a function Cons :: a -> List a -> List a. It takes two arguments, an 'a' and a 'List a' and returns a 'List a'. Here the type variable 'a' does something as Haskell ensures that if the first argument is an Int, then the second must be a List Int and will return a List Int. If the first argument is a String however, then the second must be a List String and will return a List String.
> I think a gloss of what 'λe' actually means would help
λe means "create an anonymous function that takes an argument called e with any type". And (λe -> e + 1) is an anonymous function that adds one to every element passed to it.
Seriously, check out Learn You a Haskell.