aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--ch19/19_a_1.hs31
2 files changed, 32 insertions, 1 deletions
diff --git a/README.md b/README.md
index 7c9dee0..7e8213b 100644
--- a/README.md
+++ b/README.md
@@ -180,7 +180,7 @@ are prefixed with 'Module_'.
| 18_a_2 | yes | | |
| 18_a_3 | yes, in 18_a_2 | | |
| **_18_b_1_** | yes | 441 | |
-| **_19_a_1_** | | 462 | 19. Error handling |
+| **_19_a_1_** | yes | 462 | 19. Error handling |
| **_19_b_1_** | | 465 | |
| 19_b_2 | | | |
| 19_b_3 | | | |
diff --git a/ch19/19_a_1.hs b/ch19/19_a_1.hs
new file mode 100644
index 0000000..877ca57
--- /dev/null
+++ b/ch19/19_a_1.hs
@@ -0,0 +1,31 @@
+-- Take the Either example and made it work with laziness in the style of the
+-- Maybe example.
+
+
+-- I chose the non-Monadic version of the Either example because the monadic one
+-- required changes to make it work with recent GHC versions.
+
+{-- From examples/examples/ch19/divby7.hs and modified --}
+data DivByError a = DivBy0
+ | ForbiddenDenominator a
+ deriving (Eq, Read, Show)
+
+divBy :: Integral a => a -> [a] -> [Either (DivByError a) a]
+divBy numerator denominators =
+ map worker denominators
+ where worker 0 = Left DivBy0
+ worker 10 = Left (ForbiddenDenominator 10)
+ worker 20 = Left (ForbiddenDenominator 20)
+ worker x = Right (numerator `div` x)
+{-- End of code from examples --}
+
+
+-- ghci> :l 19_a_1.hs
+-- [1 of 2] Compiling Main ( 19_a_1.hs, interpreted )
+-- Ok, one module loaded.
+
+-- ghci> take 8 (divBy 50 [1..])
+-- [Right 50,Right 25,Right 16,Right 12,Right 10,Right 8,Right 7,Right 6]
+
+-- ghci> divBy 50 [1,0,5,10,30,20]
+-- [Right 50,Left DivBy0,Right 10,Left (ForbiddenDenominator 10),Right 1,Left (ForbiddenDenominator 20)]