If I write:
b = 2I am defining a function that always returns the value 2. Haskell actually has no variables. If I subsequently try to change the value of "b", Haskell will get upset. We cannot have two definitions for the same function. No variables and no loops -- what kind of brave new world is this?
Functions can have 0 or more arguments. Here is a function with one argument:
square x = x * xHere is a function with 2 arguments:
addemup a b = a + bYou don't get to write functions with a multi-line body that eventually calls return (or potentially calls return in several places). Return in haskell has a utterly different purpose (just to confuse you). A way to add some complexity to functions is to use "let/in" compute intermediate values, such as in this rather contrived example
dist dx dy = let xx = dx * dx yy = dy * dy in xx + yyAnother syntax to do more or less the same thing uses "where":
dist dx dy = xx + yy where xx = dx * dx yy = dy * dyNote that you have to be careful and precise with indentation when you have more than one assignment in the where or in clause.
is13 13 = True is13 x = FalseA long list of patterns can be given. They are all tried in order until one works. It is a bad thing to call a function with an argument it cannot handle. For example this code:
gbu 1 = "good" gbu 2 = "bad" main = putStrLn $ gbu 7This blows up with the following error (and a long backtrace)
pat.hs: pat.hs:(4,1)-(5,13): Non-exhaustive patterns in function gbuHaskell errors can be long and frightening, but they are actually very helpful and Haskell tries to give you as much information as possible to help you fix the problem.
Tom's software pages / tom@mmto.org