People who have programmed before are welcome to join in too, but it's going to be... different for them. I realise that the first lessons will seem incredibly simple to you, but I recommend you join in right away anyway, because it will help you to unlearn the things that you really do need to unlearn to understand the later ones.
Lessons will be posted irregularly, and they will have a format much like this one: An introductory text, and a bunch of exercises. I will wait until at least one person has posted the answers to the exercises (go ahead and post them in this thread) before moving on to the next lesson.
----
An expression is a mathematical construct. It evaluates to a value. I will be using "<>" as shorthand for "evaluates to" here. "--", which is Haskell syntax, stands for "the rest of the line is a description of what's going on here". "==" stands for "equals".
Code: Select all
3 + 5 <> 8 -- adding up two numbers evaluates to another number
4 <> 4 -- a number evaluates to itself
4 == 5 <> False -- asking if one thing is the same as another evaluates to a truth value, aka a Bool
3 + 5 == 8 <> True -- an expression is the same as its value
Code: Select all
seven = 2 + 5 -- the name "seven" is now bound
14 / seven <> 14 / (2 + 5)
<> 2
A function is an expression that takes arguments (expressions), aka parameters. Functions are written in two parts: A list of formal parameters, and a function body. Formal parameters also occur inside a function body: When a function is applied to arguments, the formal parameters in the function body are bound to the respective given arguments (so, when evaluating by hand, you just replace the former with the latter). The list of formal parameters is thrown away as it becomes unnecessary.
"\", aka lambda (it's actually supposed to be t it's easier to type \), stands for "what comes next is the list of formal parameters", while "->" stands for "what comes next is the function body":
The syntax for applying a function to arguments is simple: Just write out the function (or its name), then its arguments. Once you install a Haskell implementation this will be all you need to do to use a function: We're only doing things the hard way in this first lesson.
Code: Select all
\x -> x + 5 -- this is a function that adds 5 to a given argument
(\x -> x + 5) 2 <> 2 + 5 -- a function can be used directly...
<> 7
square = \x -> x * x -- ... or given a name by binding it with "="
square seven <> (\x -> x * x) 7 -- bound names can be given as arguments just like any other expression
<> 7 * 7
<> 49
energy = \mass velocity -> 0.5 * mass * velocity * velocity -- a function can have several arguments
energy 5 2 <> 0.5 * 5 * 2 * 2
<> 10
When a function, given arguments, is evaluated to a value, it is said to return that value.
Function definitions can be written in shorthand by writing the list of formal parameters before the "=" and the function body after it, and leaving out the "\" and the "->":
Code: Select all
energy mass velocity = 0.5 * mass * velocity * velocity -- this is the same as the earlier definition
Code: Select all
max x y = if x > y then x else y
Exercises:
Evaluate these expressions by hand. No intermediate steps needed, just the final value (I will tell you if it's wrong and we can work it out together):
1. 5 + 6
2. (\x -> x + 6) 5
average x y = (x + y) / 2
3. average 2 4
4. average (max seven 10) 0
5. average 2 4 == 3
Also, define a function that takes one argument, and if that argument is less than 0, returns it negated, otherwise unchanged.
(some supplemental exercises: If that last exercise seems difficult to you, try to implement a couple of simpler functions at first. For instance, try implementing the three functions whose bodies will be the bits that you need to fit around the if-then-else expression: negative (check if its argument is smaller than 0), negate (negate its argument by putting a minus sign in front of it) and identity (return its argument unchanged). Actually, I'll just give that last function to you: It's \x -> x.)
Finally, go to Haskell.org and look around as you like.
In the next lesson, we will (among other things):
- install and configure a Haskell implementation so that you can have your computer evaluate expressions for you
- get started on learning what that cryptic heading of haskell.org means: We will be using types and higher-order functions
- recurse!