صفحه 1:
Guards
Guards
صفحه 2:
; or conditions are used to express various cases in the defi
iction.
Example
maxi :: Int -> Int -> Int
maxi nm
| << مد =n
| otherwise =m
Let’s test this function.
Main> maxi 3 2
3
Here is a trace of the function:
maxi3 2
29 3 < 2 < ۳
=3
maxi 2 5
29 2 << 5 = False
?? Otherwise = True Guards 2
صفحه 3:
yeneral form of function definitions using gu
function name pl p2
pn
| gl =el
| g2 = e2
| otherwise = e
Guards
صفحه 4:
maximum then the first guard will succeed
d be the
Example:
maxiOf3 :: Int -> Int -> Int->
Int
maxiOf3 mn p
|m>=n &&m>=p
=m
| << و
If m is th
and the result Withherwise
If the first guard fails then either n or p shou
maximum and this is what the second guard is checking.
If the second guard succeeds then the result will be n.
Otherwise, if both (first and second) guards fail then the
last guard will cause p to be returned as maximum of
4
the three arguments. Guards
صفحه 5:
ets test
maxiOf3.
Main> maxiOf3 95 4
9
True && True = True
False && False = False
maxiOf3 mn p
|m>=n &&n>=p
=m
|n>=p
=n
| otherwise
2201013 4
29 9 << 5 66 5 << 4
=9
maxiOf3 1 2 5
221 << 2 66 2 << 5
202 << 5 = False
?? otherwise = True
=5
صفحه 6:
\nother Example:
دهع مه سک
mnp
allEqual 2 3 4
=(3 = = 2) && (3 = = 4)
=False && False
=False
m n Pp
allEqual (maxil5) 5 (maxi 4 2)
= (5 = = (maxi 1 5)) && (5 = = (maxi 4 2))
901 <> 5 = False
?? otherwise = True
=(5 == 5) && (5 = = (maxi 4 2))
??4>=2 =True
=(5==5) &&(5==4)
= True && False
ب False Guards
صفحه 7:
Error Messages
Guards
صفحه 8:
The script on the next slide
contains errors
Y We will first use Notepad to create a file containing
the script
Y We will then use Hugs to interpret this script and
find the errors
Y We will use Notepad again to edit the file and correct
the errors
Y We will also test our fungtigns by evaluating some,
expressions
صفحه 9:
add3 :: Int -> Int -> Int -> Int
add3nmp=n+m+p
sig :: Int -> Int
sig a_number
|amumber <0 < 1
|amumber==0 = 0
| otherwise = 1
abs :: Int -> Int
abs x
Ix<0
| otherwise
Int
answe:
answer = 42 Guards
صفحه 10:
Il ILI UM WL.» Hugs 98: Based on the Haskell 98 standard
ال( ااا الا Il Copyright (c) 1994-2001
II---ll ات World Wide Web: http://haskell.org/hugs
۱۱ || Report bugs to: hugs-bugs@haskell.org
|| || Version: December 2001
Hugs mode: Restart with command line option +98 for Haskell 98
mode
Reading file "C:\Program Files\Hugs98\\lib\Prelude.hs":
Hugs session for:
C:\Program Files\Hugs98\\lib\Prelude.hs 10
Time -? for heln
صفحه 11:
Prelude>:1__ d:\haskell\small.hs
Reading file "d:\haskell\small.hs":
ERROR "d:\haskell\small.hs":12 - Definition of variable
"abs" clashes with import
Prelude>
The problem here is that abs is a built-in function. One
way of solving this problem is to use a different name
for our function. We will now edit the file and fix the
error.
Prelude> :e
This command ( :e ) tells Hugs to edit the last module.
Guards 11
صفحه 12:
Reading file "d:\haskell\small.hs":
Dependency analysis
ERROR "d:\haskell\small.hs":7 - Undefined variable
“a_mumber"
There is an error here to be fixed and we do that using
the :e command.
Prelude> :e
Reading file "d:\haskell\small.hs":
Hugs session for:
C:\Program Files\Hugs98\\lib\Prelude.hs
d:\haskell\small.hs
Main> Guards 12
Guards
Guards
1
s or conditions are used to express various cases in the defin
nction.
Example:
maxi :: Int -> Int -> Int
maxi n m
| n >= m
=n
| otherwise
=m
Let’s test this function.
Main> maxi 3 2
3
Here is a trace of the function:
n
m
maxi 3 2
?? 3 > 2 = True
=3
maxi 2 5
?? 2 >= 5
= False
?? Otherwise = True
=5
Guards
2
general form of function definitions using gua
function_name p1
… pn
| g1
= e1
| g2
= e2
p2
…
| otherwise = e
Guards
3
Example:
maxiOf3 :: Int -> Int -> Int ->
Int
maxiOf3 m n p
| m >= n && m >=p
=m
| n >= p
=n
If m is the maximum then the first guard will succeed
| otherwise
and the result will
be m.
= p fails then either n or p should be the
If the first guard
maximum and this is what the second guard is checking.
If the second guard succeeds then the result will be n.
Otherwise, if both (first and second) guards fail then the
last guard will cause p to be returned as maximum of
Guards
4
the three arguments.
maxiOf3 m n p
Let’s test
maxiOf3.
| m >= n && n >=p
=m
| n >= p
=n
Here| otherwise
is a trace of the function:
=p
m
n
Main> maxiOf3
9 5 4
9
p
maxiOf3 9 5 4
?? 9 >= 5 && 5 >= 4
= True && True = True
=9
maxiOf3 1 2 5
?? 1 >= 2 && 2 >= 5
?? 2 >= 5
= False && False = False
= False
?? otherwise = True
Guards
5
Another Example:
allEqual :: Int -> Int -> Int -> Bool
allEqual m n p = (n = = m) && (n = = p)
m n p
allEqual 2 3 4
=(3 = = 2) && (3 = = 4)
=False && False
=False
m
n
p
allEqual (maxi 1 5) 5 (maxi 4 2)
= (5 = = (maxi 1 5)) && (5 = = (maxi 4 2))
?? 1 >= 5
= False
?? otherwise = True
= (5 = = 5) && (5 = = (maxi 4 2))
?? 4 >= 2
= True
= (5 = = 5) && (5 = = 4)
= True && False
Guards
= False
6
Error Messages
Guards
7
The script on the next slide
contains errors
We will first use Notepad to create a file containing
the script
We will then use Hugs to interpret this script and
find the errors
We will use Notepad again to edit the file and correct
the errors
We will also test our functions by evaluating some
Guards
8
expressions
add3 :: Int -> Int -> Int -> Int
add3 n m p = n + m + p
sig :: Int -> Int
sig
a_number
| a_number < 0
= -1
| a_mumber == 0
=
0
| otherwise
=
1
abs :: Int -> Int
abs x
|x<0
= x * -1
| otherwise = x
answer :: Int
answer = 42
Guards
9
__ __ __ __ ____ ___
_________________________________________
|| || || || || || ||__
Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__|| __||
||---||
___||
|| ||
Copyright (c) 1994-2001
World Wide Web: http://haskell.org/hugs
Report bugs to: hugs-bugs@haskell.org
|| || Version: December 2001
_________________________________________
Hugs mode: Restart with command line option +98 for Haskell 98
mode
Reading file "C:\Program Files\Hugs98\\lib\Prelude.hs":
Hugs session for:
C:\Program Files\Hugs98\\lib\Prelude.hs
Guards
10
Prelude> :l
d:\haskell\small.hs
Reading file "d:\haskell\small.hs":
ERROR "d:\haskell\small.hs":12 - Definition of variable
"abs" clashes with import
Prelude>
The problem here is that abs is a built-in function. One
way of solving this problem is to use a different name
for our function. We will now edit the file and fix the
error.
Prelude> :e
This command ( :e ) tells Hugs to edit the last module.
Guards
11
Reading file "d:\haskell\small.hs":
Dependency analysis
ERROR "d:\haskell\small.hs":7 - Undefined variable
"a_mumber"
There is an error here to be fixed and we do that using
the :e command.
Prelude> :e
Reading file "d:\haskell\small.hs":
Hugs session for:
C:\Program Files\Hugs98\\lib\Prelude.hs
d:\haskell\small.hs
Main>
Guards
12