Last active
July 22, 2020 14:40
-
-
Save htsign/157c7919847c90fbe4cc7bbb4b87b1c1 to your computer and use it in GitHub Desktop.
SWI-Prolog Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list(N, N, [N]). | |
list(S, E, L) :- | |
S < E, | |
succ(S, X), | |
list(X, E, L1), | |
L = [S | L1]. | |
list(S, E, L) :- | |
S > E, | |
succ(X, S), | |
list(X, E, L1), | |
L = [S | L1]. | |
is_prime(N) :- | |
N =:= 1; N >= 2, | |
succ(P, N), | |
forall(between(2, P, X), not(0 is N mod X)). | |
:- | |
list(100, 1, L), | |
findall(N, (member(N, L), is_prime(N)), Xs), | |
writeln(Xs). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string_reverse("", ""). | |
string_reverse(S, S) :- string_length(S, 1). | |
string_reverse(S, R) :- | |
string_concat(S1, S2, S), | |
string_length(S1, 1), | |
string_reverse(S2, R1), | |
string_concat(R1, S1, R). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string_reverse(S, R) :- | |
string_to_list(S, L), | |
reverse(L, L1), | |
string_to_list(R, L1). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment