summaryrefslogtreecommitdiff
path: root/progs/demo/add.hs
blob: bdfcc2ff0a01584b77101a95d520322027859bbd (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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