mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
[prolog/en] Corrected statement about unifying two free terms (#3033)
* Corrected statement about unifying two free terms While the intricacies of unification would bring us too far, stating that assigning two free 'sides' is wrong. I tried to give a small description about how this works (without going into the details of occurrence checks or unification of more complex structures). * Fixed indentation * Replaced old style of structured comments
This commit is contained in:
parent
7d303e5042
commit
eefc0a9c92
@ -66,7 +66,10 @@ magicNumber(42).
|
||||
% follows:
|
||||
% If both sides are bound (ie, defined), check equality.
|
||||
% If one side is free (ie, undefined), assign to match the other side.
|
||||
% If both sides are free, abort because this can't be resolved.
|
||||
% If both sides are free, the assignment is remembered. With some luck,
|
||||
% one of the two sides will eventually be bound, but this isn't
|
||||
% necessary.
|
||||
%
|
||||
% The = sign in Prolog represents unification, so:
|
||||
|
||||
?- 2 = 3. % False - equality test
|
||||
@ -86,6 +89,10 @@ magicNumber(42).
|
||||
?- 5 is X+2. % Error. Unlike =, the right hand side of IS
|
||||
% must always be bound, thus guaranteeing
|
||||
% no attempt to solve an equation.
|
||||
?- X = Y, X = 2, Z is Y + 3. % X = Y, Y = 2, Z = 5.
|
||||
% X = Y are both free, so Prolog remembers
|
||||
% it. Therefore assigning X will also
|
||||
% assign Y.
|
||||
|
||||
% Any unification, and thus any predicate in Prolog, can either:
|
||||
% Succeed (return True) without changing anything,
|
||||
@ -230,9 +237,9 @@ nearby3(X,Y) :- nearby2(X,Y).
|
||||
|
||||
% Here is the structured comment declaration for nearby3:
|
||||
|
||||
%% nearby3(+X:Int, +Y:Int) is semideterministic.
|
||||
%% nearby3(+X:Int, -Y:Int) is multi.
|
||||
%% nearby3(-X:Int, +Y:Int) is multi.
|
||||
%! nearby3(+X:Int, +Y:Int) is semideterministic.
|
||||
%! nearby3(+X:Int, -Y:Int) is multi.
|
||||
%! nearby3(-X:Int, +Y:Int) is multi.
|
||||
|
||||
% For each variable we list a type. The + or - before the variable name
|
||||
% indicates if the parameter is bound (+) or free (-). The word after
|
||||
@ -267,8 +274,8 @@ character(darthVader). % Creates atom value darthVader
|
||||
% Note that below, writeln is used instead of print because print is
|
||||
% intended for debugging.
|
||||
|
||||
%% countTo(+X:Int) is deterministic.
|
||||
%% countUpTo(+Value:Int, +Limit:Int) is deterministic.
|
||||
%! countTo(+X:Int) is deterministic.
|
||||
%! countUpTo(+Value:Int, +Limit:Int) is deterministic.
|
||||
countTo(X) :- countUpTo(1,X).
|
||||
countUpTo(Value, Limit) :- Value = Limit, writeln(Value), !.
|
||||
countUpTo(Value, Limit) :- Value \= Limit, writeln(Value),
|
||||
@ -281,7 +288,7 @@ countUpTo(Value, Limit) :- Value \= Limit, writeln(Value),
|
||||
% IF test. If Value = Limit fails the second declaration is run.
|
||||
% There is also a more elegant syntax.
|
||||
|
||||
%% countUpTo2(+Value:Int, +Limit:Int) is deterministic.
|
||||
%! countUpTo2(+Value:Int, +Limit:Int) is deterministic.
|
||||
countUpTo2(Value, Limit) :- writeln(Value),
|
||||
Value = Limit -> true ; (
|
||||
NextValue is Value+1,
|
||||
@ -294,14 +301,15 @@ countUpTo2(Value, Limit) :- writeln(Value),
|
||||
% called a "failure-driven loop" to do this, but newer ones use a higher
|
||||
% order function.
|
||||
|
||||
%% countTo2(+X:Int) is deterministic.
|
||||
%! countTo2(+X:Int) is deterministic.
|
||||
countTo2(X) :- forall(between(1,X,Y),writeln(Y)).
|
||||
|
||||
?- countTo2(10). % Outputs 1 to 10
|
||||
|
||||
% Lists are given in square brackets. Use memberchk to check membership.
|
||||
% A group is safe if it doesn't include Joker or does include Batman.
|
||||
%% safe(Group:list(atom)) is deterministic.
|
||||
|
||||
%! safe(Group:list(atom)) is deterministic.
|
||||
safe(Group) :- memberchk(joker, Group) -> memberchk(batman, Group) ; true.
|
||||
|
||||
?- safe([robin]). % True
|
||||
|
Loading…
Reference in New Issue
Block a user