-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
105 lines (80 loc) · 2.16 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{-# LANGUAGE RankNTypes #-}
-- Fast Food Restaurant
module Main where
data Food =
Hamburger
| Hotdog
| Salad
deriving (Eq, Show)
data Drink =
Coke
| Pepsi
| Water
deriving (Eq, Show)
data Order =
FoodOrder Food
| DrinkOrder Drink
deriving (Show)
data Meal =
Meal Food Drink
deriving (Show)
-- Parenthesis
{-
a -> b -> c
a -> (b -> c)
(a -> (b -> c))
(a -> b -> c)
(a -> b) -> c
((a -> b) -> c)
opposite for function application
f :: (a -> b) -> c -> d
g :: a -> b
f g c
(f g) c
((f g) c)
(f g c)
f (g c)
(f (g c))
-}
-- No polymorphism or Rank 0
pairing :: Food -> Drink
pairing Hamburger = Coke
pairing Hotdog = Pepsi
pairing Salad = Water
-- Polymorphism or Rank 1
id' :: a -> a
id' a = a
length' :: [a] -> Int
length' [] = 0
length' (_:xs) = 1 + length' xs
-- Bounded (Still Rank 1) Polymorphism
type Pricer = Show a => a -> Int
always2Price :: Pricer
always2Price item = 2
lengthPrice :: Pricer
lengthPrice item = length (show item)
twiceLengthPrice :: Pricer
twiceLengthPrice item = 2 * lengthPrice item
-- forall quantifier
id'' :: forall a . a -> a
id'' a = a
-- Rank 2 Polymorphism
type Pricer' = (forall a . Show a => a -> Int)
priceMeal :: Pricer' -> Meal -> Int
priceMeal pricer (Meal f d) = pricer f + pricer d
priceMealProduct :: Pricer' -> Meal -> Int
priceMealProduct pricer (Meal f d) = pricer f * pricer d
-- Rank 3...
type PriceMeal = Pricer' -> Meal -> Int
superSizer :: PriceMeal -> PriceMeal
superSizer pm = \ p m -> (pm p m) * 2
compareModifiedSumAndProductPrices :: (PriceMeal -> PriceMeal) -> Meal -> (Int, Int)
compareModifiedSumAndProductPrices modifier meal = ((modifier priceMeal) lengthPrice meal, (modifier priceMealProduct) always2Price meal)
main = do
print (pairing Hamburger)
print (pairing Hotdog)
print (always2Price Hotdog)
print (priceMeal always2Price (Meal Hotdog Coke))
print (priceMeal lengthPrice (Meal Hotdog Coke))
print (superSizer priceMeal lengthPrice (Meal Hotdog Coke))
print (compareModifiedSumAndProductPrices superSizer (Meal Hotdog Coke))