user485498
user485498

Reputation:

Correct way to define a function in Haskell

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?

Upvotes: 9

Views: 5456

Answers (2)

Ingo
Ingo

Reputation: 36339

A 3rd way to write this would be using guards:

lucky n
    | n == 7    = "lucky"
    | otherwise = "unlucky"

There is no reason to be confused about that. There is always more than 1 way to do it. Note that this would be true even if there were no pattern matching or guards and you had to use the if.

All of the forms we've covered so far use so-called syntactic sugar provided by Haskell. Pattern guards are transformed to ordinary case expressions, as well as multiple function clauses and if expressions. Hence the most low-level, unsugared way to write this would be perhaps:

lucky n = case n of
     7 -> "lucky"
     _ -> "unlucky"

While it is good that you check for idiomatic ways I'd recommend to a beginner that he uses whatever works for him best, whatever he understands best. For example, if one does (not yet) understand points free style, there is no reason to force it. It will come to you sooner or later.

Upvotes: 7

Xion
Xion

Reputation: 22770

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:

lucky::(Integral a)=> a-> String
lucky 7 = "LUCKY NUMBER 7"
lucky _ = "Bad luck"

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:

len [] = 0

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 len would be the following:

len (x:xs) = 1 + len xs

Here, you are matching on the pattern (x:xs). Colon : is the so-called cons operator: it is appending a value to list. An expression x:xs is 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, since xs can also be an empty list ([]).

This second definition of len is 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:

len (_:xs) = 1 + len xs

which again signifies that you do not care what the first element is, only that it exists).

Upvotes: 15

Related Questions