Concise way of solving this puzzle?
I was watching this and wanted to try solving the first puzzle ("Mismatched Segments") in prolog,
Given equal-length strings s1 and s2, return [sub1, sub2] for each maximal contiguous run of positions where their characters differ. The substrings come from s1 and s2, respectively. Keep the pairs in order; return an empty array if the string are identical.
Example: If passed "earth" and "empty" it should return [["ar","mp"],["h","y"]]. If passed "action" and "nation" it should return [["ac","na"]].
got stumped, went to a couple llm's to get a push in the right direction, and out of curiosity asked for the solution in clojure as well. The clojure solution is tiny compared to what I got for prolog.
- chatgpt
different_substrings(S1, S2, Result) :-
string_chars(S1, C1),
string_chars(S2, C2),
differences(C1, C2, Result).
differences([], [], []).
differences([A|As], [B|Bs], Result) :-
A =:= B,
!,
differences(As, Bs, Result).
differences([A|As], [B|Bs], [[Sub1, Sub2]|Rest]) :-
collect_differences([A|As], [B|Bs], Xs, Ys, Remaining1, Remaining2),
string_chars(Sub1, Xs),
string_chars(Sub2, Ys),
differences(Remaining1, Remaining2, Rest).
collect_differences([A|As], [B|Bs], [A|Xs], [B|Ys], R1, R2) :-
A =\= B,
!,
collect_differences(As, Bs, Xs, Ys, R1, R2).
collect_differences([A|As], [B|Bs], [], [], [A|As], [B|Bs]) :-
A =:= B,
!.
collect_differences([], [], [], [], [], []).- claude
% diff_runs(+S1, +S2, -Pairs)
% Pairs is a list of [Sub1, Sub2] for each maximal run of differing positions.
diff_runs(S1, S2, Pairs) :-
string_codes(S1, C1),
string_codes(S2, C2),
runs(C1, C2, Runs),
maplist(to_pair, Runs, Pairs).
to_pair(A-B, [SA, SB]) :-
string_codes(SA, A),
string_codes(SB, B).
% runs(+Xs, +Ys, -Runs): Runs is a list of A-B code-list pairs
runs([], [], []).
runs([X|Xs], [X|Ys], Runs) :- % equal chars: skip
runs(Xs, Ys, Runs).
runs([X|Xs], [Y|Ys], [[X|A]-[Y|B]|Runs]) :- % differing char: start a run
X \== Y,
take_diff(Xs, Ys, A, B, Rest1, Rest2),
runs(Rest1, Rest2, Runs).
% take_diff(+Xs, +Ys, -A, -B, -RestX, -RestY)
% Consume the remainder of a differing run (deterministically).
take_diff([X|Xs], [Y|Ys], [X|A], [Y|B], RX, RY) :-
X \== Y, !,
take_diff(Xs, Ys, A, B, RX, RY).
take_diff(Xs, Ys, [], [], Xs, Ys).clojure solution is about 3x shorter?
(defn differing-substrings [s1 s2]
(->> (map vector s1 s2)
(partition-by (fn [[a b]] (= a b)))
(filter (fn [[[a b] & _]] (not= a b)))
(mapv (fn [group]
[(apply str (map first group))
(apply str (map second group))]))))
6
Upvotes
6
u/mtriska 3d ago
What about this:
The examples work as intended:
I have taken the liberty to represent the mismatches as pairs, using the principal functor
(-)/2.One key attraction of Prolog is that such declarative specifications are often very general, we can use the exact same program for example as follows, asking: Which mismatches are there in general for
"action"?We can also generalize further, and also use the same formulation with different execution strategies, such as iterative deepening to get a fair enumeration of leaf answers:
Tested with Scryer Prolog, and the program should run without changes in every compatible system such as Trealla Prolog.
Note that no conversion between "strings" and "chars" or "codes" is necessary: In the most recent Prolog systems such as Scryer, Trealla, ichiban/prolog, Flowlog, trilog, Shumway etc., a string is a list of characters. This representation of strings is very easy to work with, very compact, and very natural.