So I’m writing these notes as a kind of alternative study to the (good) tutorial “Haskell for C Programmers” by Eric Etheridge.
Some syntax is simple. Numbers:
[1, 2, 3]
“This is a string”
Tuples, which are kind of like C structs or Pascal records:
( 1, “name”, 5.0 )
The difference between tuples and lists is that tuples are always the same length, but can be different types. Lists are any length, but all the same type. They have totally different uses in Haskell.
Starting here, all code is code as it appears in a source code file. These files end in “.hs” by the way. To use the code, you load it into Hugs98, and then you can call the code from the command prompt. The command prompt is indicated with “Main>”.
Functions are defined with the = sign:
foo x = [ 1, 2 ]
Now, this is a nonsense function. It’s called like this:
Main> foo 4
[ 1, 2 ]
It always returns the same values, [ 1, 2 ].
The function name is “foo”. The argument is called “x”. The return value is always the list [ 1, 2 ].
Here’s an equally foolish function:
foo x = [ 1, 2, x ]
Call it like this:
Main> foo 9
[1, 2, 9]
It substitutes the value of x for the last item in the list.
Here’s a more useful function:
square x = x * x
Main> square 9
Yet more useful functions:
tax p = p * 0.0975
tip p = p * 0.20
totalTab p = p + (tax p) + (tip p)
While the popular tutorial starts with the example of a function that generates the list of Fibonacci numbers, I will do somthing simpler: filters on lists.
Here’s a function that takes a list as an argument and returns the entire list. This is not a filter 🙂
notafilter lst = [ x | x <- lst ]
This is a list comprehension — a statement that describes a list. This list is [ x ], where x is taken from lst (which is the argument to the function). The thing on the left of | is the an expression, and the thing on the right describes the element. In this function, the expression is just x. It could be a more complex expression.
Here’s a function that will filter in all the even numbers in a list of numbers:
evensFilter lst = [ x | x <- lst, mod x 2 == 0 ]
The expression after the comma (,) is a conditional. If its value is true, the element is included in the output list.
“mod x 2” is x modulus 2. Even numbers evaluate to 0.
== is the comparison operator.
Here’s a function that adds an “s” to each string:
pluralize lst = [ x ++ “s” | x <- lst ]
Main> pluralize [ “cat” , “dog” ]
And another one that turns verbs into nouns. Some verbs that is:
gerundize lst = [ x ++ “ing” | x <- lst ]
Main> gerundize [ “park”, “crash”, “turn”, “run”, “smoke” ]
OK, so it’s not that clever, but it’s not bad for a one-line program.
And, finally, because this tutorial is on the web, here’s a little html tag writing code:
blink str = tag “blink” str
tag t s = “<” ++ t ++ “>” ++ s ++ “</” ++ t ++ “>”
And a few more:
p s = tag “p” s
h1 s = tag “h1” s
h2 s = tag “h2” s
strong s = tag “strong” s
em s = tag “em” s
br = “<br />”
Main> p (“foo” ++ em “bar” ++ “baz”)
The parens set the order of operations. This example might be useful.
FYI, Haskell syntax notes.