forked from mmalita/PrologPuzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifthen.pl
43 lines (39 loc) · 1.04 KB
/
ifthen.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* File: ifthen.pl Author: If Then (sol MM)
Title: The false Statements
1. Only one of these statements is false
2. Only two of these statements is false
3. Only three of these statements is false
4. Only four of these statements is false
5. All five of these statements is false
Which if any is true?
?- start.
I = [false,false,false,true,false] ;
*/
:- encoding(utf8).
:- use_module(library(lists)).
start :- find(I), write(I).
/* mem(Lr,L). Elements from Lr are all members in L.
Or: Fill an empty list with elements from another list.
*/
mem([],_Y).
mem([H|T],Y) :-
member(H,Y),
mem(T,Y).
find([A1,A2,A3,A4,A5]) :-
mem([A1,A2,A3,A4,A5],[true,false]),
Sol = [A1,A2,A3,A4,A5],
count(false,Sol,N),
((A1 -> N=1 ) ; (\+ A1 -> N\=1)),
((A2 -> N=2 ) ; (\+ A2 -> N\=2)),
((A3 -> N=3 ) ; (\+ A3 -> N\=3)),
((A4 -> N=4 ) ; (\+ A4 -> N\=4)),
((A5 -> N=5 ) ; (\+ A5 -> N\=5)).
/* count(A,L,N). Counts occurrences
?- count(a,[b,a,c,d,a],N).
N = 2
*/
count(_A,[],0).
count(A,[A|L],N) :-
count(A,L,N1),
N is N1 + 1, !.
count(A,[_|L],N) :- count(A,L,N).