Содержание
-
Охрана, локальные выражения
Лекция №7
-
Охрана (guards)
bmiTell :: (RealFloat a) => a -> String bmiTell bmi | bmi
-
bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height | weight / height ^ 2 bmiTell 85 1.90 "You're supposedly normal. Pffft, I bet you're ugly!"
-
max' :: (Ord a) => a -> a -> a max' a b | a > b = a | otherwise = b max' :: (Ord a) => a -> a -> a max' a b | a > b = a | otherwise = b
-
myCompare :: (Ord a) => a -> a -> Ordering a `myCompare` b | a > b = GT | a == b = EQ | otherwise = LT ghci> 3 `myCompare` 2 GT
-
Локальные выражения (Where?)
bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height | bmi
-
bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height | bmi
-
Where and pattern matching
... where bmi = weight / height ^ 2 (skinny, normal, fat) = (18.5, 25.0, 30.0)
-
initials :: String -> String -> String initials firstname lastname = [f] ++ ". " ++ [l] ++ "." where (f:_) = firstname (l:_) = lastname
-
calcBmis :: (RealFloat a) => [(a, a)] -> [a] calcBmis xs = [bmi w h | (w, h)
-
Let it be!
-
Let it be
let in
-
cylinder :: (RealFloat a) => a -> a -> a cylinder r h = let sideArea = 2 * pi * r * h topArea = pi * r ^2 in sideArea + 2 * topArea
-
ghci> [if 5 > 3 then "Woo" else "Boo", if 'a' > 'b' then "Foo" else "Bar"] ["Woo", "Bar"] ghci> 4 * (if 10 > 5 then 10 else 0) + 2 42
-
ghci> 4 * (let a = 9 in a + 1) + 2 42 ghci> [let square x = x * x in (square 5, square 3, square 2)] [(25,9,4)]
-
ghci> (let a = 100; b = 200; c = 300 in a*b*c, \ let foo="Hey "; bar = "there!" in foo ++ bar) (6000000,"Hey there!")
-
Let it be with pattern matching
ghci> (let (a,b,c) = (1,2,3) in a+b+c) * 100 600
-
calcBmis :: (RealFloat a) => [(a, a)] -> [a] calcBmis xs = [bmi | (w, h)
-
calcBmis :: (RealFloat a) => [(a, a)] -> [a] calcBmis xs = [bmi | (w, h) = 25.0]
-
Let it be in interaction mode
ghci> let zoot x y z = x * y + z ghci> zoot 3 9 2 29 ghci> let boot x y z = x * y + z in boot 3 4 2 14 ghci> boot :1:0: Not in scope: `boot'
-
Case expressions
case expression of pattern -> result pattern -> result pattern -> result ...
-
describeList :: [a] -> String describeList xs = "The list is " ++ case xs of [] -> "empty." [x] -> "a singleton list." xs -> "a longer list."
-
describeList :: [a] -> String describeList xs = "The list is " ++ what xs where what [] = "empty." what [x] = "a singleton list." what xs = "a longer list."
-
Over
Нет комментариев для данной презентации
Помогите другим пользователям — будьте первым, кто поделится своим мнением об этой презентации.