Download presentation
Presentation is loading. Please wait.
Published byEustacia McBride Modified over 9 years ago
1
Guards1
2
2 Let’s test this function. Main> maxi 3 2 3 Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases in the definition of a function. Example: maxi :: Int -> Int -> Int maxi n m | n >= m = n | otherwise = m ?? 3 > 2 = True = 3 maxi 2 5 ?? 2 >= 5 = False ?? Otherwise = True = 5
3
Guards3 function_name p1 p2 … pn | g1 = e1 | g2 = e2 … | otherwise = e The general form of function definitions using guards:
4
Guards4 Example: maxiOf3 :: Int -> Int -> Int -> Int maxiOf3 m n p | m >= n && m >=p = m | n >= p = n | otherwise = p If 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.
5
Guards5 Here 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 =5 Let’s test maxiOf3. Main> maxiOf3 9 5 4 9 maxiOf3 m n p | m >= n && n >=p = m | n >= p = n | otherwise = p
6
Guards6 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 = False Another Example:
7
Guards7 Error Messages
8
Guards8 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 expressions The script on the next slide contains errors
9
Guards9 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
10
Guards10 __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ 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 Type :? for help Prelude>
11
Guards11 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.
12
Guards12 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> We should now be able to use these functions in our expressions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.