Skip to content

Instantly share code, notes, and snippets.

@vituscze
Created March 13, 2025 23:59
Show Gist options
  • Save vituscze/cde3493c27c500363bf01331f14c56cd to your computer and use it in GitHub Desktop.
Save vituscze/cde3493c27c500363bf01331f14c56cd to your computer and use it in GitHub Desktop.
sublist(L, M) :-
M = [_|_],
append(_, Suffix, L),
append(M, _, Suffix).
subseq([], []).
subseq([X|L], [X|M]) :- subseq(L, M).
subseq([_|L], M) :- subseq(L, M).
disjoint([], [], []).
disjoint([X|L], [X|M], N) :- disjoint(L, M, N).
disjoint([X|L], M, [X|N]) :- disjoint(L, M, N).
rotate([], []).
rotate(L, M) :- same_length(L, M), append(A, [X|B], L), append([X|B], A, M).
uncons([A|B], A, B).
split_matrix(M, Col, Rest) :- M = [_|_], maplist(uncons, M, Col, Rest).
% nebo pomoci transpose; z prednasky nebo clpfd
rotate_matrix(M, R) :-
split_matrix(M, Col, Rest) -> reverse(Col, RCol), R = [RCol|T], rotate_matrix(Rest, T);
append(M, []), R = [].
spiral(M, S) :-
split_matrix(M, Col, Rest) -> rotate_matrix(Rest, Rotated), spiral(Rotated, RS), append(Col, RS, S);
S = [].
length_flip(Len, List) :- length(List, Len).
matProm(M, N, Mat) :- length(Mat, M), maplist(length_flip(N), Mat).
all_dif([]).
all_dif([X|Xs]) :- maplist(dif(X), Xs), all_dif(Xs).
add_one(X, Y, R, CIn, COut) :-
between(0, 9, X),
between(0, 9, Y),
Total is X + Y + CIn,
R is Total mod 10,
COut is Total div 10.
soucet(A, B, C) :-
append([A, B, C], All),
sort(All, Vars),
all_dif(Vars),
maplist(reverse, [A, B, C], [RA, RB, RC]),
foldl(add_one, RA, RB, RC, 0, 0).
gen_expr([Val], Val).
gen_expr(Vals, Expr) :-
L = [_|_], R = [_|_],
append(L, R, Vals),
gen_expr(L, LExpr),
gen_expr(R, RExpr),
member(Op, [+,-,*]),
Expr =.. [Op, LExpr, RExpr].
arit(Vals, Res, Expr) :- gen_expr(Vals, Expr), Res is Expr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment