:- module main.
:- interface.
:- use_module io.
:- pred main(io.io::di, io.io::uo) is det.
:- implementation.
:- use_module string.
:- use_module exception.
:- import_module int.
:- import_module list.
:- pred get_int(string::in, int::out, io.io::di, io.io::uo) is det.
get_int(Name, Out, !IO) :-
io.write_string("Please enter ", !IO),
io.write_string(Name, !IO),
io.nl(!IO),
io.read_line_as_string(LineResult, !IO),
(
( LineResult = io.error(_) ; LineResult = io.eof ),
exception.throw(exception.software_error("IO Error"))
;
LineResult = io.ok(Line),
( if
string.to_int(string.strip(Line), Int)
then
Out = Int
else
io.write_string("Not an integer.\n", !IO),
get_int(Name, Out, !IO)
)
).
:- pred print_speed(int::in, int::in, io.io::di, io.io::uo) is det.
print_speed(Speed, Hour) -->
io.write_string("Hours: "),
io.write_int(Hour), io.write_char(('\t')),
io.write_string("Distance: "),
io.write_int(Distance), io.nl,
{ Distance = Hour * Speed }.
main(!IO) :-
get_int("speed", Speed, !IO),
get_int("hours", Hours, !IO),
list.foldl(print_speed(Speed), 0 .. Hours, !IO).