You are on page 1of 7

Experiment No.

Aim: Introduction to Pattern Matching and Guards.

Theory:

(a) Pattern Matching


• The left-hand side of the defining equation has the form of an application.
• If the argument in that application is a name (for example x=x*x),then when an
application is evaluated, the name will take on the argument value and the right side of
the function is calculated.
• If the argument on the left-hand side is a constant, then the right-hand side will be used
only if the function is applied to that value.

Example

(1) ∶: ⟶
1= “ "
2= “ ”
3= “ ℎ ”

(2) _ ℎ ∶: ⟶
_ℎ 3 =
_ℎ =

Note:- Given the application, _ ℎ (1 + 1) the argument is evaluated to 2. Since this


does not match 3, the second defining equation is used.

(3) ∶: ⟶ ⟶
=
$ =

Note:- A function may have more than one argument, then each argument in the given
defining equation is checked from left to right.

1
• Following Inbuilt Functions are defined under pattern matching:

Examples:

(1) ∷ ( , $) ⟶
( , ') =

(2) ( ∷ ( , $) ⟶ $
( ( , ') = '

• An argument pattern may also be list.


• Since a list is either [ ] or else is constructed with the : (+ ) operator, the pattern
takes the form [ ] and ∶ .

Note:- By convention, list arguments usually have an “s” suffix in their name. For eg:-
, etc.

Examples:

(1) ,-. ' ∷ [ ] ⟶


,-. ' [ ] =
,-. ' ( : ) =

• When ,-. '[ ] is evaluated, the assignment matches the pattern and the output is
True.
• When ,-. ' (1: 2: 3: [ ] ) is evaluated, the output is False.

(2) ℎ ( ∷ [ ] ⟶
ℎ (( : ) =

(3) ∷ [ ] ⟶
( : ) =

Note:- : patterns must be parenthesised, because application has priority over (: ).

2
• The sequence of the defining equation in a function are important as it effects the
output.

Examples:

(1) '- ∶ : ( ) => −>


'- 1 = “ ”
'- 2 = “ ”
'- 3 = “ ℎ ”
'- = “ $ 1 & 3”

Note:- If the last pattern is moved to the top, it would always say “not between 1 & 3”
because it would catch all the numbers and the other patterns will not be matched at all
even if 1, 2 or 3 is entered.

• Functions can often be defined in many different ways using pattern matching.

Examples:

(1) (&&) ∶ : −> −>


&& =
&& =
&& =
&& =

Can be defined more compactly by:

(2) && =

_ && _ =

However, the following definition is more efficient because it avoids evaluating the
second argument, if the first argument is False.

(3) && $ = $
&& _ =

3
Note:- The underscore symbol ( _ ) is a wildcard pattern that matches any argument value.

(b) Conditional Expression


• As in most programming languages, functions can be defined using conditional
expression:

Examples:

(1) $ 1 ∶ : (2 ( , 3 - ) => −>


$ 1 =
<0
ℎ –

(2) -1 ∶ : (2 ( , 3 - ) => −>


-1 =
<0
ℎ −1
== 0
ℎ 0
1

(c) Concept of Guards

• Guards are a way of testing whether some property of a value (or several of them) is
true or false.
• It is similar to an if statement. The difference is that guards make the code far better
and more readable.

Examples:

(1) $- ∶ : (7 ) => −> −>

$- $-

4
| $- <= 18.5 = “ ( ℎ”

| $- <= 25.0 = “ - ”

| $- <= 30.0 = “ ”

| ℎ = “ < ( -$ ”

• Guards are indicated by pipes that follows a function’s name and its parameter.
• They are indented a bit to the right and lined up.
• It is a Boolean expression.
• If it evaluates to true, then the corresponding function body is used.
• If it evaluates to false, checking drops through to the next guard and so on.

Note:- The last guard is otherwise and is simply defined as otherwise = True and
catches everything.

• The bmi function can be modified in the following manner where the user just needs
to enter his weight and height and the BMI will be calculated.

Examples:

(1) $- ∶ : (7 ) ⟹ ⟶ ⟶
$- ℎ ℎ ℎ
| ℎ /ℎ ℎ ^2 <= 18.5 = “ ( ℎ ”

| ℎ /ℎ ℎ ^2 <= 25.5 = “ - ”

| ℎ /ℎ ℎ ^2 <= 30.0 = “ ”

| ℎ =“ < ( -$ ”

• In the above example, the formula for calculating the bmi is repeated for every guard.
To overcome the repetition, where clause can be used.

(2) $- ∶ : (7 ) ⟹ ⟶ ⟶

5
$- ℎ ℎ ℎ
| $- <= 18.5 = “ ( ℎ”

| $- <= 25.0 = “ - ”

| $- <= 30.0 = “ ”

| ℎ = “ < ( -$ ”

ℎ $- = ℎ /ℎ ℎ ^2

Note:- Put the keyword where after the guards (usually its best to indent it as much as the
pipes are indented) and then define several names or functions.

• Other examples of guards and where clause:


(1) -'@ -. ∶ : (2 ( ) => −> −> 2 (
`-'@ -. `$
| >$= B
| == $ = ,C
| ℎ = D

(2) ∶: −> −>

- - = [ ] ++“.” ++[ ] ++ “.”

ℎ ( : _) = -
( : _) = -

Reading Exercise:-

Study about the following functions:


a) Let clause in Haskell
b) Case expression in Haskell.

6
c) Difference between let and while.

Exercise:-

1. Write a function to check whether the supplied input is 7 or not.


2. Write a function that accepts the character ‘a’ and displays the string “Albert”, for ‘b’
displays “Bruno”, for ‘c’ display “Cecil” and displays an error message for any other input
character.
3. Write a function to add two vectors using the concept of pattern matching.
4. Write a function for a 3-tuple using the concept of pattern matching for performing the
following tasks:
(a) Extract only the first element.
(b) Extract only the second element.
(c) Extract only the third element.

(function similar to fst and snd that operates on a pair)

You might also like