The most important thing you can know about Haskell and Functional Programming
The Haskell equations
f x y = ... used in the definition
of a function
f are genuine mathematical equations. They
state that the left hand side of the right hand side of the equation
have the same value. This is very different from the use of
= in imperative languages like C or Java. In a C or Java
program the statement
x = x*y does not mean that
x*y have the same value, but rather it
is a command to throw away the old value of
x and put the
x*y in its place. It is a so-called
destructive assignment statement: the old value of a variable
is destroyed and replaced by a new one.
Reasoning about Haskell definitions is a lot easier than reasoning
about programs that use destructive assignment. In Haskell, standard
reasoning about mathematical equations applies. E.g. after the Haskell
x= 1 and
y = 2, the Haskell
x = x + y will raise an error
multiply defined. ...
= in Haskell has
the meaning "is by definition equal to"...
This was a huge landslide victory for me. Because I quit trying to write programs to get data here, data there. Values here, values there. Instead, I simply began to rewrite the original function as a new definition.
I became so confident that I was able to write a program to return all the leaves of a tree. and here it is:
Looking back at "Algorithms in Haskell" by Rabhi and "Craft of FP" by Simon Thompson, they do both make this same statement, but somehow it never really hit me right.