mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-19 03:13:40 +00:00
If we want to encourage people to read the sample programs
and learn Mercury programming from them, they should not be written
in an obsolete style.
samples/beer.m:
samples/calculator.m:
samples/calculator2.m:
samples/concurrency/midimon/midimon.m:
samples/diff/diff_out.m:
samples/e.m:
samples/eliza.m:
samples/muz/dict.m:
samples/muz/higher_order.m:
samples/muz/muz.m:
samples/muz/typecheck.m:
samples/muz/word.m:
samples/muz/zabstract.m:
samples/muz/zlogic.m:
samples/muz/zparser.m:
samples/muz/ztoken.m:
samples/muz/ztoken_io.m:
samples/muz/ztype.m:
samples/muz/ztype_op.m:
samples/rot13/rot13_concise.m:
samples/rot13/rot13_gustavo.m:
samples/rot13/rot13_juergen.m:
samples/rot13/rot13_ralph.m:
samples/rot13/rot13_verbose.m:
samples/solutions/all_solutions.m:
samples/solutions/n_solutions.m:
samples/solutions/one_solution.m:
samples/solutions/some_solutions.m:
samples/solver_types/eqneq.m:
samples/solver_types/sudoku.m:
samples/solver_types/test_eqneq.m:
Replace uses of __ as module qualifier with dot.
Replace (C->T;E) with (if C then T else E).
Use our usual indentation for if-then-elses and for switches.
Import one module per line. Put those imports into alphabetical order.
Replace many uses of DCGs with state variables, leaving DCGs
mostly just for parsing code.
Use predmode declarations where this helps.
Put predicates in top-down order where relevant.
Use io.format where this helps.
Do not put more than one predicate call on one line.
Put each function symbol in a du type on a separate line.
Put spaces after commas, around the bar in list syntax,
around arithmetic operators, and around minus signs used for pairs.
Replace tab indentation with four-space indentation.
Delete spaces at the ends of lines.
Replace two or more consecutive blank lines with one blank line.
Delete blank lines that do not help structure the code.
There are probably still some examples of old practices remaining;
I do not claim to have fixed them all.
74 lines
2.4 KiB
Mathematica
74 lines
2.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1995-1999 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% file: higher_order.m
|
|
% main author: philip
|
|
|
|
:- module higher_order.
|
|
:- interface.
|
|
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
% :- func univ(string, list(term)) = term.
|
|
%
|
|
% :- func const(string) = term.
|
|
%
|
|
% :- func term_from_list(func(X) = term, list(X)) = term.
|
|
% :- mode term_from_list(func(in) = out is det, in) = out is det.
|
|
%
|
|
% :- func term_from_string(string) = term.
|
|
%
|
|
% :- func term_from_int(int) = term.
|
|
%
|
|
% :- func maybe_to_term(func(X) = term, maybe(X)) = term.
|
|
% :- mode maybe_to_term(func(in) = out is det, in) = out is det.
|
|
%
|
|
% :- func pair_to_term(func(X) = term, func(Y) = term, pair(X, Y)) = term.
|
|
% :- mode pair_to_term(func(in) = out is det, func(in) = out is det, in) = out
|
|
% is det.
|
|
|
|
:- func string_portray_list(func(X) = string, list(X)) = string.
|
|
:- mode string_portray_list(func(in) = out is det, in) = out is det.
|
|
|
|
:- func string_portray_list(func(X) = string, string, string, string, list(X))
|
|
= string.
|
|
:- mode string_portray_list(func(in) = out is det, in, in, in, in) = out
|
|
is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module builtin.
|
|
:- import_module int.
|
|
|
|
% univ(S, L) = term.functor(term.atom(S), L, C) :- term.context_init(C).
|
|
%
|
|
% const(S) = term.functor(term.atom(S), [], C) :- term.context_init(C).
|
|
%
|
|
% term_from_list(_, []) = const("[]").
|
|
% term_from_list(P, [H|L]) = univ("[|]", [apply(P, H), term_from_list(P, L)]).
|
|
%
|
|
% term_from_string(S) = term.functor(term.string(S), [], C) :-
|
|
% term.context_init(C).
|
|
%
|
|
% term_from_int(I) = term.functor(term.integer(I), [], C) :-
|
|
% term.context_init(C).
|
|
%
|
|
% maybe_to_term(_, no) = const("no").
|
|
% maybe_to_term(P, yes(X)) = univ("yes", [apply(P, X)]).
|
|
%
|
|
% pair_to_term(P1, P2, X1-X2) = univ("-", [apply(P1, X1), apply(P2, X2)]).
|
|
|
|
string_portray_list(F, L) = string_portray_list(F, "[", ", ", "]", L).
|
|
|
|
string_portray_list(F, Pre, In, Post, L) = S :-
|
|
list.map((pred(I::in, O::out) is det :- O = F(I)), L, SL),
|
|
list.length(SL, N),
|
|
list.duplicate(N - 1, In, InL),
|
|
list.append(InL, [Post], InL1),
|
|
list.zip(SL, InL1, SL1),
|
|
string.append_list([Pre | SL1], S).
|