Simple arithmetic
x = 18 - 7
main = print xGotcha with negative numbers - the follwing won't work,
x = 10 * -5
main = print xBut this will
x = 10 * (-5)
main = print xBoolean logic
x = True && False
y = False || True
z = 7 == (10 - 3)
main = print (x, y)Notice here that the print statement printed a single value,
but that value was a a pair of parentheses,
containing a comma separated list of values,
in this case, x, y, and z.
This is called a pattern. More on this later.
Comparing different types
x = "derp" == 1337
main = print xx = "derp" == "herp"
main = print xAs expected, the latter works, but the former fails to compile.
That is because, the Haskell compiler does not know how to compare (or add, or subtract) a number with a string.
Functions are values
All the values so far are actually Functions
x = 25Is actually defining a function x,
which takes no parameters, and always evaluates to 5.
(Note that I say "evaluates to", not "returns".)
Functions with parameters
x a b = a + b
main = print $ x 3 4This defines a function x,
which takes in two parameters, a and b,
and evaluates it to the sum of a and b.
x (a, b) = a + b
main = print $ x 3 4Notice that in the first definition,
there were no parentheses surrounding x's only parameter -
they are simply not required in Haskell,
unlike most other programming languages.
This one does not compile, because
this defines a function x
which takes in just one parameter, (a, b),
which happens to be a tuple containing two values.
x (a, b) = a + b
main = print $ x (3, 4)Thus, this would be the correct way to invoke it.