-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdinburgh5.hs
More file actions
24 lines (21 loc) · 825 Bytes
/
Copy pathEdinburgh5.hs
File metadata and controls
24 lines (21 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
--- 1
data Fruit = Apple(String, Bool) | Orange(String, Int)
deriving (Show)
isBloodOrange :: Fruit -> Bool
isBloodOrange (Apple (_,_)) = False
isBloodOrange (Orange (x,_)) | x `elem` ["Tarocco", "Moro", "Sanguinello"] = True
| otherwise = False
--- 2
bloodOrangeSegments :: [Fruit] -> Int
bloodOrangeSegments [] = 0
bloodOrangeSegments (fruit:fruitList)
| isBloodOrange fruit = case fruit of Orange (_,segments) -> segments + bloodOrangeSegments fruitList
| otherwise = bloodOrangeSegments fruitList
--- 3
worms :: [Fruit] -> Int
worms [] = 0
worms (f:fruitList)
| containsWorm f = 1 + worms fruitList
| otherwise = worms fruitList
where containsWorm (Apple (_,True)) = True
containsWorm f = False