دانلود فایل

# پاور پوینت (اسلاید) Guards - دانلود فایل

دانلود فایل پاور پوینت (اسلاید) Guards

دانلود فایل پاور پوینت (اسلاید) Guards

محتواب برخی از اسلاید ها :

4Example:maxiOf3 :: Int -> Int -> Int -> Int
maxiOf3 m n p
| m >= n && m >=p = m
| n >= p = n
| otherwise = pIf m is the maximum then the first guard will succeed and the result will be m.
If the first guard fails then either n or p should be the 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 the three arguments.5Here is a trace of the function:
m n p
maxiOf3 9 5 4
9 >= 5 && 5 >= 4 = True && True = True
= 9
maxiOf3 1 2 5
1 >= 2 && 2 >= 5 = False && False = False
2 >= 5 = False
otherwise = True
=5Let’s test maxiOf3.
Main> maxiOf3 9 5 4
9maxiOf3 m n p
| m >= n && n >=p = m
| n >= p = n
| otherwise = p6allEqual :: 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
= FalseAnother Example:7Error Messages8We 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 expressions The script on the next slide contains errors9add3 :: 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 = 4210__ __ __ __ ____ ___ _________________________________________
|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__|| __|| Copyright (c) 1994-2001
||---|| ___|| World Wide Web: http://haskell.org/hugs
|| || Report bugs to: [email protected]
|| || 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
Type : for help
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. 12Reading 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

Hugs session for:
C:\Program Files\Hugs98\\lib\Prelude.hs
Main>
We should now be able to use these functions in our expressions.