--
--
-- IO
--
--

--
-- IO
--
print x :=
  do write x
     write "\n"
     flush ()

printToPort port x :=
  do writeToPort port x
     writeToPort port "\n"

display x :=
  do write x
     flush ()

displayToPort port x := do writeToPort port x

eachLine proc :=
  do let eof := isEof ()
     if eof
       then return ()
       else do let line := readLine ()
               proc line
               eachLine proc

eachLineFromPort port proc :=
  do let eof := isEofPort port
     if eof
       then return ()
       else do let line := readLineFromPort port
               proc line
               eachLineFromPort port proc

eachFile files proc :=
  match files as list string with
    | [] -> return ()
    | $file :: $rest ->
      do let port := openInputFile file
         eachLineFromPort port proc
         closeInputPort port
         eachFile rest proc

--
-- Collection
--
each proc xs :=
  match xs as list something with
    | [] -> do return ()
    | $x :: $rs ->
      do proc x
         each proc rs

--
-- Debug
--
debug %expr :=
  io do print (show expr)
        return expr

debug2 %msg %expr :=
  io do display msg
        print (show expr)
        return expr
