Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pp:2024:l11 [2024/05/19 23:57] tpruteanu created |
pp:2024:l11 [2025/05/09 20:59] (current) ldaniel fix typo at fmap definition |
||
---|---|---|---|
Line 23: | Line 23: | ||
We can declare a ''Functor'' instance for any abstract type ''f a'' which has the ability to 'map' over it's value, while preserving the structure of ''f''. | We can declare a ''Functor'' instance for any abstract type ''f a'' which has the ability to 'map' over it's value, while preserving the structure of ''f''. | ||
- | Declaring a instance ''Functor'' instance for ''f'', allows us to use functions relating to mapping for any type ''f a''. | + | Declaring ''Functor'' instance for ''f'', allows us to use functions relating to mapping for any type ''f a''. |
For ''fmap'' to have a predictible behaviour, any ''Functor'' instance we define needs to obey the **Functor Laws**: | For ''fmap'' to have a predictible behaviour, any ''Functor'' instance we define needs to obey the **Functor Laws**: | ||
Line 46: | Line 46: | ||
<note> | <note> | ||
- | You can think of Monads as **containers** that keep a variable inside a context. We will see several examples in the next sectinos. | + | You can think of Monads as **containers** that keep a variable inside a context. We will see several examples in the next sections. |
</note> | </note> | ||
Line 66: | Line 66: | ||
<code haskell> | <code haskell> | ||
instance Functor Foo where | instance Functor Foo where | ||
- | fmap f fx = px >>= (\x -> return (f x)) | + | fmap f fx = fx >>= (\x -> return (f x)) |
| | ||
instance Applicative Foo where | instance Applicative Foo where | ||
Line 76: | Line 76: | ||
==== Do notation ==== | ==== Do notation ==== | ||
- | You can probably notice a lot of monadic code uses a lot of binds and lambda functions. Take for example the following snippet that uses monads to unpack 2 ''Maybe'' values and add them. | + | You can probably notice a lot of monadic code uses binds and lambda functions. Take for example the following snippet that uses monads to unpack 2 ''Maybe'' values and add them. |
<code haskell> | <code haskell> | ||
add ma mb = ma >>= (\a -> | add ma mb = ma >>= (\a -> | ||
Line 99: | Line 99: | ||
fmap f x = | fmap f x = | ||
case x of | case x of | ||
- | Just v -> f v | + | Just v -> Just (f v) |
Nothing -> Nothing | Nothing -> Nothing | ||
Line 145: | Line 145: | ||
This means a ''main'' function is just a function that interacts with the external world. \\ | This means a ''main'' function is just a function that interacts with the external world. \\ | ||
- | You can use the commandline utility ''runhaskell main.hs'' to quickly test your Haskell programs without directly compiling them. | + | You can use the commandline utility ''runhaskell main.hs'' to quickly test your Haskell programs without compiling them. |
</note> | </note> | ||
Line 175: | Line 175: | ||
</hidden> | </hidden> | ||
- | What is the context a list puts a value in? **Non-determinism**, a List represent the fact that the value can have multiple values. | + | What is the context a list puts a value in? **Non-determinism**, a list represent the fact that the object can have multiple possible values. |
We will take a simple example where the list monad might be very helpful. Conside a 8x8 chess board. A position would be: | We will take a simple example where the list monad might be very helpful. Conside a 8x8 chess board. A position would be: | ||
Line 208: | Line 208: | ||
<code haskell> | <code haskell> | ||
- | newtype Prob = Prob [(a, Float)] deriving Show | + | newtype Prob a = Prob [(a, Float)] deriving Show |
</code> | </code> | ||
Line 253: | Line 253: | ||
coin :: Prob Coin | coin :: Prob Coin | ||
- | coin = [(Heads, 0.5), (Tails, 0.5)] | + | coin = Prob [(Heads, 0.5), (Tails, 0.5)] |
unfair_coin :: Prob Coin | unfair_coin :: Prob Coin | ||
- | unfair_coin = [(Heads, 0.6), (Tails, 0.4)] | + | unfair_coin = Prob [(Heads, 0.6), (Tails, 0.4)] |
flip :: Prob [Coin] | flip :: Prob [Coin] | ||
Line 271: | Line 271: | ||
die n = undefined | die n = undefined | ||
- | -- (die 20) <*> (die 6) -- the probability distribution of rolling a d20 followed by a d6 | + | -- (,) <$> (die 20) <*> (die 6) -- the probability distribution of rolling a d20 followed by a d6 |
</code> | </code> | ||
**11.4.5.** Let's use this framework to solve a M3 problem: "Jo has took a test for a disease. The result of the test is either positive or negative and the test is 95% reliable: in 95% of cases of people who really have the disease, a positive result is returned, and in 95% of cases of people who do not have the disease, a negative result is obtained. 1% of people of Jo’s age and background have the disease. Jo took the test, and the result is positive. What is the probability that Jo has the disease?" | **11.4.5.** Let's use this framework to solve a M3 problem: "Jo has took a test for a disease. The result of the test is either positive or negative and the test is 95% reliable: in 95% of cases of people who really have the disease, a positive result is returned, and in 95% of cases of people who do not have the disease, a negative result is obtained. 1% of people of Jo’s age and background have the disease. Jo took the test, and the result is positive. What is the probability that Jo has the disease?" |