Re:Why Does “=” Mean Assignment?
by derefr on Tuesday April 10, 2018 @05:25PM
(#16805607)
Also valid in Erlang:
Oh, and here's a fun one:
If you're wondering what the use of that is, here's an Erlang "drop all occurrences of an element X from a list" function. (Prerequisite knowledge for this: Erlang functions have several clause-heads; which one is executed on each call depends on which one is able to successfully bind to—i.e. = with—the arguments.)
In other words—first, set up an accumulator. Then, go through the list, and if X can bind in both positions, skip the element; otherwise (i.e. if Y != X) then shift Y into the accumulator. At the end, reverse the accumulator (because you were shifting.)
[ Parent
| on HN ]
To illustrate how close Erlang syntax is to Prolog, here is drop/3 in Prolog:
drop(X, List, Result) :- drop(X, List, [], Result).
drop(_, [], Acc, Result) :- reverse(Acc, Result).
drop(X, [X|Rest], Acc, Result) :- drop(X, Rest, Acc, Result).
drop(X, [Y|Rest], Acc, Result) :- dif(X, Y), drop(X, Rest, [Y|Acc], Result).
I only had to make a few syntactic changes to the original program to obtain a Prolog predicate from it. The most significant change is that I use an additional argument to hold the original function's return value. An important consequence of the resulting relational nature is that this can answer quite general queries.For example, we can ask: What are possible solutions Ls that arise when we drop X from the single-element list [A] ?
?- drop(X, [A], Ls).
X = A, Ls = [] ;
dif(X, A), Ls = [A].
We see that there are two possible answers: One where X is A, and hence the result is the empty list []. And the other where X is different from A, as indicated by dif(X, A).Even more generally, we can systematically enumerate all conceivable solutions for all list lengths:
?- length(Ls0, _), drop(X, Ls0, Ls).
Ls0 = Ls, Ls = [] ;
Ls0 = [X],
Ls = [] ;
Ls0 = Ls, Ls = [_586],
dif(X, _586) ;
Ls0 = [X, X],
Ls = [] ;
Ls0 = [X, _598],
Ls = [_598],
dif(X, _598) ;
etc.
This uses iterative deepening to generate all possible answers.Also not stuck using only an unbound result. Could we not ask "given L and Ls, what elements were dropped?"
Yes, that's also possible with this definition! For example: Given Ls0 = [a,b,c] and Ls = [a,c], which element (if any) was dropped:
?- drop(X, [a,b,c], [a,c]).
X = b ;
false.
As another example, if both Ls0 and Ls are [a,b,c]: ?- drop(X, [a,b,c], [a,b,c]).
dif(X, c),
dif(X, b),
dif(X, a).
This means that X must be different from each of these elements.More generally, which elements can be dropped at all from the list [a,b,c]:
?- drop(X, [a,b,c], _).
X = a ;
X = b ;
X = c ;
dif(X, c),
dif(X, b),
dif(X, a).
Interestingly, the predicate definition does not even use "=" in its clauses. In Prolog, (=)/2 is a built-in predicate that means unification. In the definition above, we use implicit unification instead of explicit unification.I have mixed feelings on this. On one hand I did want this notation while learning haskell. Partly because there are a bunch of places in haskell's type system where repeating a variable implicitly gives equality constraints.
On the other hand something like
drop _ [] = []
drop x (y:xs)
| x == y = drop x xs
| otherwise = y : drop x xs
is often more readable at a glance to me since I don't have to parse variable names to see control flow structures.Also, in statically typed languages what happens if equality doesn't work for a value.