7. h' = 60
hue (Mix c c') = let
h = hue c
h' = hue c'
m = average h h' m = 180
m' = norm (m + 180) m' = 0
d = distance h m
in case compare d 90 of
LT -> m
EQ -> nan h = 300
GT -> m'
norm h | h < 360 = h
| otherwise = norm (h - 360)
28. Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci> :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci>
29. Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci> :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci> :t (,,,)
30. Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci> :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci> :t (,,,)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)
ghci>
31. Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci> :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci> :t (,,,)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)
ghci> :t ()
32. Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci> :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci> :t (,,,)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)
ghci> :t ()
() :: ()
ghci>
33. Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci> :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci> :t (,,,)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)
ghci> :t ()
() :: ()
ghci> -- Trivial is similar to void.
34. Either (Sum)
Color :: *
ghci> :t (Red, Blue, Green)
(Red, Blue, Green) :: (Color, Color, Color)
ghci>Either a b = Left a | Right b
data :t (,,)
(,,) :: a -> b -> c -> (a, b, c)
ghci> :t (,,,)
(,,,) :: a -> b -> c -> d -> (a, b, c, d)
ghci> :t ()
() :: ()
ghci> -- Trivial is similar to void.
ghci>
37. hue :: Color -> Double
hue Red =0
hue Green = 120
hue Blue = 240
hue (Mix c c') = ... sometimes NaN
38. hue :: Color -> Double
hue Red =0
hue Green = 120
hue Blue = 240
hue (Mix c c') = ... sometimes nothing
39. hue :: Color -> Maybe Double
hue Red =0
hue Green = 120
hue Blue = 240
hue (Mix c c') = ... sometimes nothing
40. hue :: Color -> Maybe Double
hue Red = Just 0
hue Green = Just 120
hue Blue = Just 240
hue (Mix c c') = ... sometimes nothing
41. hue :: Color -> Maybe Double
hue Red = Just 0
hue Green = Just 120
hue Blue = Just 240
hue (Mix c c') = let
... stuff ...
in case compare d 90 of
LT -> m
EQ -> nan
GT -> m'
42. hue :: Color -> Maybe Double
hue Red = Just 0
hue Green = Just 120
hue Blue = Just 240
hue (Mix c c') = let
... stuff ...
in case compare d 90 of
LT -> Just m
EQ -> nan
GT -> Just m'
43. hue :: Color -> Maybe Double
hue Red = Just 0
hue Green = Just 120
hue Blue = Just 240
hue (Mix c c') = let
... stuff ...
in case compare d 90 of
LT -> Just m
EQ -> Nothing
GT -> Just m'
44. hue :: Color -> Maybe Double
hue Red = Just 0
hue Green = Just 120
hue Blue = Just 240
hue (Mix c c') = let
h = hue c
h' = hue c'
... stuff ...
in case compare d 90 of
LT -> Just m
EQ -> Nothing
GT -> Just m'
45. hue (Mix c c') = let
h = hue c
h' = hue c'
...
46. hue (Mix c c') = let
h = hue c
h' = hue c'
...
47. hue (Mix c c') = let
(h, h') = (hue c, hue c')
...
48. hue (Mix c c') = case (hue c, hue c') of
(h, h') -> let
...
49. hue (Mix c c') = case (hue c, hue c') of
(Just h, Just h') -> let
...
50. hue (Mix c c') = case (hue c, hue c') of
(Just h, Just h') -> let
...
(Just h, Nothing) -> ...
(Nothing, Just h') -> ...
(Nothing, Nothing) -> ...
51. hue (Mix c c') = case (hue c, hue c') of
(Just h, Just h') -> let
...
(Just h, Nothing) -> Nothing
(Nothing, Just h') -> Nothing
(Nothing, Nothing) -> Nothing
52. hue (Mix c c') = case (hue c, hue c') of
(Just h, Just h') -> let
...
_ -> Nothing
53. hue (Mix c c') = case (hue c, hue c') of
(Just h, Just h') -> let
m = average h h'
m' = norm (m + 180)
d = distance h m
in case compare d 90 of
LT -> Just m
EQ -> Nothing
GT -> Just m'
_ -> Nothing
60. Parametric
Polymorphism
(.) :: (b -> c) -> (a -> b) -> a -> c
infixr . -- defaults to 9
(f . g) x = f (g x)
isN :: Double -> Bool
isN = not . isNaN
61. Parametric
Polymorphism
(.) :: (b -> c) -> (a -> b) -> a -> c
infixr . -- defaults to 9
(f . g) x = f (g x)
isN :: Double -> Bool
isN = not . isNaN
celsiusToFahrenheit :: Double -> Double
celsiusToFahrenheit t = 9/5 * t + 32
62. Parametric
Polymorphism
(.) :: (b -> c) -> (a -> b) -> a -> c
infixr . -- defaults to 9
(f . g) x = f (g x)
isN :: Double -> Bool
isN = not . isNaN
celsiusToFahrenheit :: Double -> Double
celsiusToFahrenheit t = 9/5 * t + 32
celsiusToFahrenheit' = (32 +) . (9/5 *)
65. Parametric
Polymorphism
($) :: (a -> b) -> a -> b
infixr 0 $ -- very low precedence.
f$x=fx
-- Allows you to drop parenthesis:
n = sqrt ( abs ( cos 1))
n' = sqrt $ abs $ cos 1
99. Generators
[ (a, b, c) | a <- nats, b <- nats, c <- nats]
[(1,1,1),(1,1,2),(1,1,3),⋯]
100. Generators
[ (a, b, c) | a <- nats, b <- nats, c <- nats]
[(1,1,1),(1,1,2),(1,1,3),⋯]
(1,2,3) (1,3,2)
(2,1,3) (3,1,2)
(2,3,1) (3,2,1)
101. Generators
[ (a, b, c) | a <- nats, b <- [1..a], c <- [1..b]]
[(1,1,1),(2,1,1),(2,2,1),⋯]
102. Generators
[ (a, b, c) | c <- nats, b <- [1..c], a <- [1..b]]
[(1,1,1),(1,1,2),(1,2,2),⋯]
103. Guards
[ (a, b, c) | c <- nats, b <- [1..c], a <- [1..b],
a^2 + b^2 == c^2]
[(3,4,5),(6,8,10),(5,12,13),⋯]
104. Local Declaration
[ (a, b, c) | c <- nats, b <- [1..c], a <- [1..b],
a^2 + b^2 == c^2,
...
commonDivisors == [1]]
[(3,4,5),(5,12,13),(8,15,17),⋯]
105. Local Declaration
[ (a, b, c) | c <- nats, b <- [1..c], a <- [1..b],
a^2 + b^2 == c^2,
let commonDivisors = [d | d <- divisors a,
d `divides` b, d `divides` c],
commonDivisors == [1]]
[(3,4,5),(5,12,13),(8,15,17),⋯]
106. Local Declaration
[ (a, b, c) | c <- nats, b <- [1..c], a <- [1..b],
a^2 + b^2 == c^2,
let d = gcd a b,
d == 1]
[(3,4,5),(5,12,13),(8,15,17),⋯]
108. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
primes
109. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
sieve [2..]
110. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
sieve (2:[3..])
111. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : sieve [x | x <- [3..], rem x 2 /= 0]
112. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : sieve [x | x <- 3:[4..], rem x 2 /= 0]
113. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : sieve (3:[x | x <- [4..], rem x 2 /= 0])
114. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : sieve [x |
x <- [x | x <- [4..], rem x 2 /= 0],
rem x 3 /= 0]
115. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : sieve [x |
x <- [x | x <- 4:[5..], rem x 2 /= 0],
rem x 3 /= 0]
116. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : sieve [x |
x <- [x | x <- [5..], rem x 2 /= 0],
rem x 3 /= 0]
117. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : sieve [x |
x <- [x | x <- 5:[6..], rem x 2 /= 0],
rem x 3 /= 0]
118. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : sieve [x |
x <- 5:[x | x <- [6..], rem x 2 /= 0],
rem x 3 /= 0]
119. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : sieve (5:[x |
x <- [x | x <- [6..], rem x 2 /= 0],
rem x 3 /= 0])
120. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve [x |
x <- [x |
x <- [x | x <- [6..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0]
121. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve [x |
x <- [x |
x <- [x | x <- 6:[7..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0]
122. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve [x |
x <- [x |
x <- [x | x <- [7..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0]
123. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve [x |
x <- [x |
x <- [x | x <- 7:[8..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0]
124. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve [x |
x <- [x |
x <- 7:[x | x <- [8..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0]
125. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve [x |
x <- 7:[x |
x <- [x | x <- [8..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0]
126. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : sieve (7:[x |
x <- [x |
x <- [x | x <- [8..], rem x 2 /= 0],
rem x 3 /= 0]),
rem x 5 /= 0])
127. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
2 : 3 : 5 : 7 : sieve ⋯
128. Recursion
primes = sieve [2..] where
sieve (p:xs) =
p : sieve [x | x <- xs, rem x p /= 0]
[2, 3, 5, 7, sieve ⋯]