Exercises part 1
In these exercises, you are going to implement some small command-line applications. Some of these will potentially run forever, as they will only stop when the user enters a keyword for quitting the application. Such programs are no longer provably total. If you added the %default total
pragma at the top of your source file, you'll need to annotate these functions with covering
, meaning that you covered all cases in all pattern matches but your program might still loop due to unrestricted recursion.
-
Implement function
rep
, which will read a line of input from the terminal, evaluate it using the given function, and print the result to standard output:rep : (String -> String) -> IO ()
-
Implement function
repl
, which behaves just likerep
but will repeat itself forever (or until being forcefully terminated):covering repl : (String -> String) -> IO ()
-
Implement function
replTill
, which behaves just likerepl
but will only continue looping if the given function returns aRight
. If it returns aLeft
,replTill
should print the final message wrapped in theLeft
and then stop.covering replTill : (String -> Either String String) -> IO ()
-
Write a program, which reads arithmetic expressions from standard input, evaluates them using
eval
, and prints the result to standard output. The program should loop until users stops it by entering "done", in which case the program should terminate with a friendly greeting. UsereplTill
in your implementation. -
Implement function
replWith
, which behaves just likerepl
but uses some internal state to accumulate values. At each iteration (including the very first one!), the current state should be printed to standard output using functiondispState
, and the next state should be computed using functionnext
. The loop should terminate in case of aLeft
and print a final message usingdispResult
:covering replWith : (state : s) -> (next : s -> String -> Either res s) -> (dispState : s -> String) -> (dispResult : res -> s -> String) -> IO ()
-
Use
replWith
from Exercise 5 to write a program for reading natural numbers from standard input and printing the accumulated sum of these numbers. The program should terminate in case of invalid input and if a user enters "done".