codesnippets:monads
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:monads [2023/06/05 01:11] – f2b216 | codesnippets:monads [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ~~DISCUSSION~~ | ~~DISCUSSION~~ | ||
| + | |||
| ====== Monads ====== | ====== Monads ====== | ||
| ===== Example 1a ===== | ===== Example 1a ===== | ||
| - | * shows how a alternative results (sum type, algebraic type, see type A, B and C in the example) can be handled and propagated in a computation by means of a function that handles such alternative results | + | * this example |
| - | * that function | + | * it works but it is quite some code |
| - | * examples | + | |
| + | * Code:< | ||
| + | main :: IO () | ||
| + | main = | ||
| + | do | ||
| + | putStrLn $ show (fAListToC [(A 12), (A 7), (A 11), (A 5)] fAToB) | ||
| + | putStrLn $ show (fAListToC [(A 7), (A 13), (A 11), (A 5)] fAToB) | ||
| + | putStrLn $ show (fAListToC [(A (-1)), (A 7), (A 11), (A 5)] fAToB) | ||
| + | putStrLn $ show (fAListToC [(A 7), (A 11), (A 5), (A 13)] fAToB) | ||
| + | |||
| + | data A a = A a | AError | ANone | ||
| + | deriving Show | ||
| + | |||
| + | data B a = B a | BError | BNone | ||
| + | deriving Show | ||
| + | |||
| + | data C a = C a | CError | CNone | ||
| + | deriving Show | ||
| + | |||
| + | fAToB :: A Int -> B Int | ||
| + | fAToB (A n) | ||
| + | | n <= 0 = BNone | ||
| + | | n < 13 = B (23 `div` n) | ||
| + | | otherwise = BError | ||
| + | fAToB AError = BError | ||
| + | fAToB ANone = BNone | ||
| + | |||
| + | fAListToC :: [A a] -> (A a -> B a) -> C [a] | ||
| + | fAListToC [] _ = C [] | ||
| + | fAListToC (a@(A _):lrAList) fAToB' = | ||
| + | do | ||
| + | case fAToB' a of | ||
| + | B b -> | ||
| + | let | ||
| + | c = (fAListToC lrAList fAToB' | ||
| + | in | ||
| + | case c of | ||
| + | C lb -> C (b : lb) | ||
| + | CNone -> CNone | ||
| + | CError -> CError | ||
| + | BError -> CError | ||
| + | BNone -> CNone | ||
| + | fAListToC (AError:_) _ = CError | ||
| + | fAListToC (ANone:_) _ = CNone | ||
| + | </ | ||
| + | |||
| + | * Output:< | ||
| + | C [1,3,2,4] | ||
| + | CError | ||
| + | CNone | ||
| + | CError | ||
| + | </ | ||
| + | |||
| + | ===== Example 1b ===== | ||
| + | |||
| + | * this example does not explain monads but an alternative solution where monads usually apply | ||
| + | * it shows how alternative results (sum type, algebraic type, see type A, B and C in the example) can be handled and propagated in a computation by means of a function that handles such alternative results | ||
| + | * that function | ||
| + | |||
| + | * more detailed description of the program | ||
| + | * the function ' | ||
| + | * the transformation of data is according to function ' | ||
| + | * ' | ||
| + | * in ' | ||
| + | |||
| + | * this example | ||
| * Code:< | * Code:< | ||
| Line 28: | Line 94: | ||
| fAToB :: A Int -> B Int | fAToB :: A Int -> B Int | ||
| fAToB (A n) | fAToB (A n) | ||
| - | | n < 0 = BNone | + | | n <= 0 = BNone |
| - | | n < 13 = B (n `div` 2) | + | | n < 13 = B (23 `div` n) |
| | otherwise = BError | | otherwise = BError | ||
| fAToB AError = BError | fAToB AError = BError | ||
| Line 50: | Line 116: | ||
| ifC CError _ _ bError = bError | ifC CError _ _ bError = bError | ||
| </ | </ | ||
| + | |||
| * Output:< | * Output:< | ||
| - | C [6,3,5,2] | + | C [1,3,2,4] |
| CError | CError | ||
| CNone | CNone | ||
| Line 57: | Line 124: | ||
| </ | </ | ||
| - | ===== Example | + | ===== Example |
| * this example uses the instances for Functor, Applicative and Monad | * this example uses the instances for Functor, Applicative and Monad | ||
| Line 97: | Line 164: | ||
| fAToB :: A Int -> B Int | fAToB :: A Int -> B Int | ||
| fAToB (A n) | fAToB (A n) | ||
| - | | n < 0 = BNone | + | | n <= 0 = BNone |
| - | | n < 13 = B (n `div` 2) | + | | n < 13 = B (23 `div` n) |
| | otherwise = BError | | otherwise = BError | ||
| fAToB AError = BError | fAToB AError = BError | ||
| Line 113: | Line 180: | ||
| fAListToC (AError:_) _ = CError | fAListToC (AError:_) _ = CError | ||
| fAListToC (ANone:_) _ = CNone | fAListToC (ANone:_) _ = CNone | ||
| + | </ | ||
| - | ifC :: C a -> (a -> b) -> b -> b -> b | ||
| - | ifC (C x) fThen _ _ = fThen x | ||
| - | ifC CNone _ bNone _ = bNone | ||
| - | ifC CError _ _ bError = bError | ||
| - | </ | ||
| * Output:< | * Output:< | ||
| - | C [6,3,5,2] | + | C [1,3,2,4] |
| CError | CError | ||
| CNone | CNone | ||
| Line 242: | Line 305: | ||
| Empty s >> | Empty s >> | ||
| (Contains x) >>= worker = worker x | (Contains x) >>= worker = worker x | ||
| - | return | + | return |
| + | </ | ||
| - | Output:< | + | * Output:< |
| Contains (Wrapper (Chopsticks {nPieces = 54, nQuality = Polished})) | Contains (Wrapper (Chopsticks {nPieces = 54, nQuality = Polished})) | ||
| Contains (StorageBin (Chopsticks {nPieces = 34, nQuality = Polished})) | Contains (StorageBin (Chopsticks {nPieces = 34, nQuality = Polished})) | ||
| Line 250: | Line 314: | ||
| Empty "to short" | Empty "to short" | ||
| Empty "to small in diameter" | Empty "to small in diameter" | ||
| - | Empty "no chopsticks at packing"</ | + | Empty "no chopsticks at packing" |
| + | </ | ||
codesnippets/monads.1685920288.txt.gz · Last modified: (external edit)
