home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hugs101o.zip / Progs / HUGS / Demos / iosynch.hs < prev    next >
Text File  |  1995-02-14  |  2KB  |  35 lines

  1. -- This file contains the example program from section 7.7 of the Haskell
  2. -- report (version 1.1) for a program using synchronisation.
  3.  
  4. main :: Dialogue
  5. main  = readChan stdin abort (\userInput -> readNums (lines userInput))
  6.  
  7. readNums           :: [String] -> Dialogue
  8. readNums inputLines = readIntP "Enter first number: " inputLines
  9.                         (\num1 inputLines1 ->
  10.                           readIntP "Enter second number: " inputLines1
  11.                             (\num2 _ -> reportResult num1 num2))
  12.  
  13. reportResult       :: Int -> Int -> Dialogue
  14. reportResult num1 num2
  15.   = appendChan stdout ("Their sum is: "++ show (num1 + num2)) abort done
  16.                                   
  17.  
  18. -- readIntP prints a prompt and then reads a line of input.  If the
  19. -- line contains an integer, the value of the integer is passed to the
  20. -- success continuation.  If a line cannot be parsed as an integer,
  21. -- an error message is printed and the user is asked to try again.
  22. -- If EOF is detected, the program is aborted.
  23.  
  24. readIntP :: String -> [String] -> (Int -> [String] -> Dialogue) -> Dialogue
  25. readIntP prompt inputLines succ
  26.   = appendChan stdout prompt abort
  27.       (case inputLines of
  28.          (l1 : rest) -> case (reads l1) of
  29.                           [(n,"")] -> succ n rest
  30.                           _        -> appendChan stdout
  31.                                        "Error - retype the number\n" abort
  32.                                        (readIntP prompt rest succ)
  33.          _           -> appendChan stdout "Early EOF" abort done)
  34.  
  35.