صفحه 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