summaryrefslogtreecommitdiff
path: root/progs/demo/improved-add.hs
diff options
context:
space:
mode:
authorYale AI Dept <ai@nebula.cs.yale.edu>1993-07-14 13:08:00 -0500
committerDuncan McGreggor <duncan.mcgreggor@rackspace.com>1993-07-14 13:08:00 -0500
commit4e987026148fe65c323afbc93cd560c07bf06b3f (patch)
tree26ae54177389edcbe453d25a00c38c2774e8b7d4 /progs/demo/improved-add.hs
Import to github.
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