3 ms·
For whatever it's worth, here are Prolog implementations of these examples (the second one is specific to SWI-Prolog, there is no standardized file system libra
by tom_mellior 7y ago
For whatever it's worth, here are Prolog implementations of these examples (the second one is specific to SWI-Prolog, there is no standardized file system library, I believe).
Programming in pure Prolog is a bit like programming only in terms of the constraints inside a list comprehension.
These implementations don't actually produce lists but enumerate answers one by one, using backtracking. If you don't need all the answers, you just stop the enumeration whenever you like, and later elements are not computed. This is similar to lazy evaluation, and also works if there is an infinite sequence of answers. If you do want a list of all (finitely many) solutions, you would use one of several standard predicates (findall/3, setof/3, bagof/3) on the backtracking version. But this is needed less often than beginners think.
list_evenmember(List, EvenMember) :-
member(EvenMember, List),
EvenMember mod 2 =:= 0.
directory_minsize_file(Directory, MinSize, File) :-
directory_files(Directory, Files),
member(File, Files),
size_file(File, Size),
Size > MinSize.
abcd(A, B, C, D) :-
member(A, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
member(B, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
member(C, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
member(D, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
A =\= 0,
(1000 * A + 100 * B + 10 * C + D) * 4 =:= 1000 * D + 100 * C + 10 * B + A.