Let us make the Echo program just a little bit more interesting: We add an initial welcome phrase and a prompt with a line number. Here is the program:
module Echo2 where import POSIX echo2 env = class count := 1 prompt = do env.stdout.write (show count++"> ") count := count+1 echo str = action env.stdout.write str prompt result action env.stdin.installR echo env.stdout.write "Welcome to Echo2!\n" prompt root = newRoot echo2
The class definition now has two more components:
In addition, the interface returned upon instantiation is a dedicated method that takes care of all program-specific initialization. Such a class allows for a particularly simple root definition, as shown in the last line of the example. The wrapper function newRoot has the following definition in module POSIX:
newRoot :: (Env -> Class Action) -> World -> Cmd () () newRoot cl world = do env = new posix world init = new cl env init
Some additional details:
show is a function that converts the integer value count to a string; ++ denotes string concatenation. Both these are defined in the Prelude, a library module which is implicitly imported by all modules.
The user input string given to echo will include the newline; hence so will the string echoed to stdout. But to write the welcome phrase on a separate line, it will have to end with an explicit newline.