summaryrefslogtreecommitdiff
path: root/progs/demo/improved-add.hs
diff options
context:
space:
mode:
Diffstat (limited to 'progs/demo/improved-add.hs')
-rw-r--r--progs/demo/improved-add.hs21
1 files changed, 21 insertions, 0 deletions
diff --git a/progs/demo/improved-add.hs b/progs/demo/improved-add.hs
new file mode 100644
index 0000000..bdfcc2f
--- /dev/null
+++ b/progs/demo/improved-add.hs
@@ -0,0 +1,21 @@
+-- this is an interactive program to read in two numbers and print their sum.
+
+module Main where
+
+main = readChan stdin abort $ \userInput ->
+ let inputLines = lines userInput in
+ readInt "Enter first number: " inputLines $ \num1 inputLines1 ->
+ readInt "Enter second number: " inputLines1 $ \ num2 _ ->
+ appendChan stdout ("Their sum is: " ++ show (num1 + num2)) abort done
+
+readInt :: String -> [String] -> (Integer -> [String] -> Dialogue) -> Dialogue
+
+readInt prompt inputLines succ =
+ appendChan stdout prompt abort $
+ case inputLines of
+ (l1 : rest) -> case (reads l1) of
+ [(x,"")] -> succ x rest
+ _ -> appendChan stdout
+ "Error - retype the number\n" abort $
+ readInt prompt rest succ
+ _ -> appendChan stdout "Early EOF" abort done