Files
mercury/samples/rot13/rot13_concise.m
Sebastian Godelet 59ce15323f Fix samples/rot13_concise + libxml compilation error
boehm_gc/.gitignore:
    Ignore .obj files

extras/xml/xml.dtd.m:
extras/xml/xml.parse.m:
    Change the name of functor ('1') in the multiplicity/0 type
    to one. The former name does not (yet) work with the C# grade.

samples/.gitignore:
    Ignore library output and sample executables.

samples/c_interface/standalone_c/.gitignore:
    Ignore mercury_lib_int output files.

samples/c_interface/standalone_c/Makefile:
    chmod 0644.

samples/c_interface/.gitignore:
    Ignore object files and executables.

samples/java_interface/.gitignore:
    Ignore Java class files and executables.

samples/lazy_list/.gitignore:
samples/muz/.gitignore:
samples/rot13/.gitignore:
    Ignore executables.

samples/rot13/rot13_concise.m:
    Update call to index_det to use string.det_index.
    Added comments to clarify type inference.

samples/solutions/.gitignore:
    Ignore executables.

samples/solutions/all_solutions.m:
samples/solutions/n_solutions.m:
samples/solutions/one_solution.m:
samples/solutions/some_solutions.m:
    chmod 0644.

samples/solver_types/.gitignore:
    Ignore executables.

samples/solver_types/sudoku.m:
    Output the solution in a 3x3 grid.
2014-03-11 11:30:00 +11:00

66 lines
1.5 KiB
Mathematica

% File: rot13_concise.m
% Main authors: Warwick Harvey <wharvey@cs.monash.edu.au>
% Fergus Henderson <fjh@cs.mu.oz.au>
%
% rot13_concise:
%
% Program to read its input, apply the rot13 algorithm, and write it out
% again.
%
% This version is more concise (but less efficient) than its companion,
% rot13_verbose.
%
% Compile with: mmc --make --infer-all
%
% Key features:
% - is independent of character set (e.g. ASCII, EBCDIC)
% - has proper error handling
%
:- module rot13_concise.
:- interface.
:- import_module io.
:- pred main(state, state).
:- mode main(di, uo) is det.
:- implementation.
:- import_module char, int, string.
% The length of `alphabet' should be a multiple of `cycle'.
% Inferred declaration: func alphabet = string.
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
% Inferred declaration: func cycle = int.
cycle = 26.
% Inferred declaration: func rot_n(int, char) = char.
rot_n(N, Char) = RotChar :-
char_to_string(Char, CharString),
( if sub_string_search(alphabet, CharString, Index) then
NewIndex = (Index + N) mod cycle + cycle * (Index // cycle),
string.det_index(alphabet, NewIndex, RotChar)
else
RotChar = Char
).
% Inferred declaration: func rot13(char) = char.
rot13(Char) = rot_n(13, Char).
main -->
read_char(Res),
( { Res = ok(Char) },
print(rot13(Char)),
main
; { Res = eof }
; { Res = error(ErrorCode) },
{ error_message(ErrorCode, ErrorMessage) },
stderr_stream(StdErr),
print(StdErr, "rot13: error reading input: "),
print(StdErr, ErrorMessage),
nl(StdErr)
).