From 4e987026148fe65c323afbc93cd560c07bf06b3f Mon Sep 17 00:00:00 2001 From: Yale AI Dept Date: Wed, 14 Jul 1993 13:08:00 -0500 Subject: Import to github. --- progs/demo/improved-add.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 progs/demo/improved-add.hs (limited to 'progs/demo/improved-add.hs') 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 -- cgit v1.2.3