% ghciUh? What is this strange type? ~~~ Num a => a -> a -> a ~~~ First, let's focus on the right part `a -> a -> a`. To understand it, just look at a list of progressive examples: | The written type | Its meaning | | `Int` | the type `Int` | | `Int -> Int` | the type function from `Int` to `Int` | | `Float -> Int` | the type function from `Float` to `Int` | | `a -> Int` | the type function from any type to `Int` | | `a -> a` | the type function from any type `a` to the same type `a` | | `a -> a -> a` | the type function of two arguments of any type `a` to the same type `a` | In the type `a -> a -> a`, the letter `a` is a _type variable_. It means `f` is a function with two arguments and both arguments and the result have the same type. The type variable `a` could take many different type value. For example `Int`, `Integer`, `Float`... So instead of having a forced type like in `C` with declaring the function for `int`, `long`, `float`, `double`, etc... We declare only one function like in a dynamically typed language. Generally `a` can be any type. For example a `String`, an `Int`, but also more complex types, like `Trees`, other functions, etc... But here our type is prefixed with `Num a => `. `Num` is a _type class_. A type class can be understood as a set of types. `Num` contains only types which behave like numbers. More precisely, `Num` is class containing types who implement a specific list of functions, and in particular `(+)` and `(*)`. Type classes are a very powerful language construct. We can do some incredibly powerful stuff with this. More on this later. Finally, `Num a => a -> a -> a` means: Let `a` be a type belonging to the `Num` type class. This is a function from type `a` to (`a -> a`). Yes, strange. In fact, in Haskell no function really has two arguments. Instead all functions have only one argument. But we will note that taking two arguments is equivalent to taking one argument and returning a function taking the second argument as parameter. More precisely `f 3 4` is equivalent to `(f 3) 4`. Note `f 3` is a function: ~~~ f :: Num a :: a -> a -> a g :: Num a :: a -> a g = f 3 g y ⇔ 3*3 + y*y ~~~ Another notation exists for functions. The lambda notation allows us to create functions without assigning them a name. We call them anonymous function. We could have written: ~~~ g = \y -> 3*3 + y*y ~~~ The `\` is used because it looks like `λ` and is ASCII. If you are not used to functional programming your brain should start to heat up. It is time to make a real application.`GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude>`

let f x y = x*x + y*y`Prelude>`

:type f`f :: Num a => a -> a -> a`