[mercury-users] Mercury programmer required ;-)
Gustavo A. Ospina
g-ospina at uniandes.edu.co
Sat Sep 26 02:21:39 AEST 1998
I have another version of rot13.
This version reads a line and prints the line "roted". I think it is as
declarative as Jurgen's version. Also I handle error and I used predicates
on your char library. Maybe my version is slower, but it can be discussed.
I'm in agree with these programming challenges. I hope these will be
presented more often.
All you have a nice day.
+ Gustavo.
+---------------------------------------+
| Gustavo A. Ospina |
| MSc Student |
| Computing and Systems Engineering |
| University of Los Andes |
| Santafe de Bogota D.C., Colombia |
| e-mail: g-ospina at uniandes.edu.co |
+---------------------------------------+
-------------------------------- Cut Here --------------------------------
:- module rot13.
:- interface.
:- import_module io.
:- pred main(io__state::di,io__state::uo) is det.
:- implementation.
:- import_module char,int,list.
:- pred rot13(char::in,char::out) is det.
rot13(Char,RotChar) :-
char__is_upper(Char) ->
rot13(Char,0'A,RotChar)
;
char__is_lower(Char) ->
rot13(Char,0'a,RotChar)
;
RotChar = Char.
:- pred rot13(char::in,int::in,char::out) is det.
rot13(Char,CodeLetterA,RotChar) :-
char__to_int(Char,CodeChar),
RotCode = (CodeChar - CodeLetterA + 13) mod 26 + CodeLetterA,
char__to_int(RChar,RotCode) ->
RotChar = RChar
;
RotChar = '\a'.
/* Alert character (Error case. To satisfy mode check) */
:- pred printRotChars(list(char)::in,io__state::di,io__state::uo) is det.
printRotChars([]) -->
io__nl.
printRotChars([Ch|Chs]) -->
{rot13(Ch,RotCh)},
io__write_char(RotCh),
printRotChars(Chs).
% Main Program
main -->
io__read_line(Result),
(
{Result = ok(Line)},
printRotChars(Line)
;
{Result = eof,
true}
;
{Result = error(Error),
io__error_message(Error,Message)},
io__stderr_stream(Stderr),
io__write_string(Stderr,Message)
).
More information about the users
mailing list