I’m new to Haskell and I’m trying out a few tutorials.
I wrote this script:
lucky::(Integral a)=> a-> String
lucky 7 = "LUCKY NUMBER 7"
lucky x = "Bad luck"
I saved this as lucky.hs and ran it in the interpreter and it works fine.
But I am unsure about function definitions. It seems from the little I have read that I could equally define the function lucky as follows (function name is lucky2):
lucky2::(Integral a)=> a-> String
lucky2 x=(if x== 7 then "LUCKY NUMBER 7" else "Bad luck")
Both seem to work equally well. Clearly function lucky is clearer to read but is the lucky2 a correct way to write a function?
They are both correct. Arguably, the first one is more idiomatic Haskell because it uses its very important feature called pattern matching. In this form, it would usually be written as:
The underscore signifies the fact that you are ignoring the exact form (value) of your parameter. You only care that it is different than
7, which was the pattern captured by your previous declaration.The importance of pattern matching is best illustrated by function that operates on more complicated data, such as lists. If you were to write a function that computes a length of list, for example, you would likely start by providing a variant for empty lists:
The
[]clause is a pattern, which is set to match empty lists. Empty lists obviously have length of 0, so that’s what we are having our function return.The other part of
lenwould be the following:Here, you are matching on the pattern
(x:xs). Colon:is the so-called cons operator: it is appending a value to list. An expressionx:xsis therefore a pattern which matches some element (x) being appended to some list (xs). As a whole, it matches a list which has at least one element, sincexscan also be an empty list ([]).This second definition of
lenis also pretty straightforward. You compute the length of remaining list (len xs) and at 1 to it, which corresponds to the first element (x).(The usual way to write the above definition would be:
which again signifies that you do not care what the first element is, only that it exists).