Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Question 1 %
- % Strange Average %
- %% implement a rule that takes two positive integers
- %% that are each between 0 and 100 (assuming inclusive),
- %% and computes their strangeAverage.
- %% the strangeAverage is the average between 0, 100, and the two inputs
- strangeAverage(X,Y,S_Average) :-
- X >= 0, X =< 100, Y >= 0, Y =< 100,
- S_Average is (X+Y+100)/4.
- % Question 2 %
- % Countdown %
- %% implement a rule that takes a positive integer,
- %% and produces a list that starts at that number and
- %% counts down to 1.
- countdown(1,[1]).
- countdown(X,[X|L2]) :- X > 1, Y is X-1, countdown(Y, L2).
- % Question 3 %
- % Related %
- %% implement a rule that returns true if prolog can prove
- %% that the first person is related to the second person.
- %% note: there is some extraneous code here because I
- %% wasn't sure of the definition of related.
- %% the extra code allows flexibility if
- %% my original premise was incorrect.
- %% related if recursively [hRelated or vRelated]
- %% hRelated if siblings and not self
- %% sibling if they share a parent
- %% vRelated if they are recursively [parent or child]
- % helper functions:
- % sibling isRecursiveChildOf isRecursiveParentOf vRelated hRelated
- %%% facts %%%
- parent(fred, mary).
- parent(mary, jim).
- parent(mary, greg).
- parent(angie, terry).
- parent(terry, marge).
- %%% rules %%%
- sibling(A,B) :- parent(C,A), parent(C,B).
- isRecursiveChildOf(P,Cx) :- parent(P, Cx).
- isRecursiveChildOf(P,Cx) :- parent(P, C), isRecursiveChildOf(C,Cx).
- isRecursiveParentOf(C,P) :- isRecursiveChildOf(P,C).
- vRelated(A,B) :- isRecursiveParentOf(A,B).
- vRelated(A,B) :- isRecursiveChildOf(A,B).
- hRelated(A,B) :- sibling(A,B), A \= B.
- related(A,B) :- hRelated(A,B).
- related(A,B) :- vRelated(A,B).
- % Question 4 %
- % Sphinx %
- %% solves the cryptarithmetic problem
- %% called the "sphinx"
- %% general direction was taken from the following resource:
- %% http://clip.dia.fi.upm.es/~vocal/public_info/seminar_notes/node13.html
- %% I worked on this problem before it was discussed in class, and
- %% searching google for "generate and test" brought this up. sorry.
- % helper functions:
- % assignAll assign numMaker
- assignAll([],_).
- assignAll([L1a|L1b], Digits) :- assign(L1a, Digits, Digits2), assignAll(L1b, Digits2).
- assign(X, [X|L], L).
- assign(X, [L1a|L1b], [L1a|L2b]) :- assign(X, L1b, L2b).
- sphinx(S,E,P,T,M,B,R) :-
- assignAll([S,E,P,T,M,B,R], [0,1,2,3,4,5,6,7,8,9]),
- S \= 0, E \= 0, M \= 0,
- numMaker([S,E,P,T,E,M,B,R,E],X1),
- numMaker([E,R,B,M,E,T,P,E,S],X2),
- numMaker([M,P,P,B,R,P,S,S,M],X3),
- X1 - X2 =:= X3.
- numMaker([L1a|L1b],Answer) :- numMaker([L1a|L1b],Iterations,Answer), Iterations > 0.
- numMaker([],Iterations,Answer) :- Iterations is -1, Answer is 0.
- numMaker([L1a|L1b],Iterations,Answer) :- numMaker(L1b,Iterations2,Answer2),
- Iterations is Iterations2+1, Answer is Answer2+(L1a*(10^Iterations)).
- % Question 5 %
- % SumPicker %
- %% takes two lists. the first list is the picker,
- %% and the second list is the data.
- %% this program sums up the numbers in the data list
- %% referenced by the picker list.
- % helper functions:
- % contains
- contains(_, [], Count) :- Count is 0.
- contains(X, [X|L2], Count) :- contains(X, L2, C), Count is C+1.
- contains(X, [L1|L2], Count) :- X \= L1, contains(X, L2, Count).
- sumPicker(L1,L2,A) :- sumPicker(1,L1,L2,A).
- sumPicker(_,_,[],A) :- A is 0.
- sumPicker(X,L1,[_|L2b],A) :- contains(X,L1,C), C < 1, Y is X+1, sumPicker(Y,L1,L2b,A2), A is A2.
- sumPicker(X,L1,[L2a|L2b],A) :- contains(X,L1,C), C >= 1, Y is X+1, sumPicker(Y,L1,L2b,A2), A is A2+L2a.
- % Question X1: Fracktorial %
- fracktorial(X, Ans) :- isEven(X, Even), fracktorial(X, Ans, Even).
- fracktorial(0, Ans, _) :- Ans is 1.
- fracktorial(X, Ans, 1) :- X > 0, Y is X-2, fracktorial(Y, Ans2, 1), Ans is Ans2*X.
- fracktorial(X, Ans, 0) :- X > 0, Y is X-1, fracktorial(Y, Ans2, 1), Ans is Ans2.
- isEven(0, Ans) :- Ans is 1.
- isEven(1, Ans) :- Ans is 0.
- isEven(X, Ans) :- X > 1, Y is X-2, isEven(Y, Ans).
- % Question X2: ReverseListHalves %
- reverseListHalves(L, A) :- count(L, Count), isEven(Count, Even), reverseListHalves(L,A,Even).
- reverseListHalves(L, A, 0) :- %if odd
- count(L, Count),
- End is Count-1, myFloor(End/2,Middle), Start is 0,
- MiddlePre is Middle-1, MiddlePost is Middle+1,
- sub(Start,MiddlePre,L,Half1), sub(MiddlePost,End,L,Half2),
- myReverse(Half1,Half1r), myReverse(Half2,Half2r),
- getIndexInL(Middle,L,MiddleElement),
- myAppend(Half1r,MiddleElement,Half1r2),
- myConcat(Half1r2,Half2r,A).
- reverseListHalves(L, A, 1) :- %if even
- count(L, Count),
- End is Count-1, Middle is End/2, Start is 0,
- sub(Start,Middle,L,Half1), sub(Middle,End,L,Half2),
- myReverse(Half1,Half1r), myReverse(Half2,Half2r),
- myConcat(Half1r,Half2r,A).
- count([], A) :- A is 0.
- count([_|L1b], A) :- count(L1b, A2), A is A2+1.
- sub(Start,End,L,A) :- sub(0,Start,End,L,A).
- sub(_,_,_,[],A).
- sub(Index,_,End,L,A) :- Index > End, A = [].
- sub(Index,Start,End,L,A) :- Index < Start, Index2 is Index+1, sub(Index2,Start,End,L,A).
- sub(Index,Start,End,L,[A2|A3]) :- Index >= Start, Index =< End, getIndexInL(Index,L,A2), Index2 is Index+1, sub(Index2,Start,End,L,A3).
- getIndexInL(Index,L,A) :- getIndexInL(Index,0,L,A).
- getIndexInL(Index,Index,[A|_],A).
- getIndexInL(Index,X,[L1a|L1b],A) :- Index \= X, X2 is X+1, getIndexInL(Index,X2,L1b,A).
- myReverse([],L2) :- L2 = [].
- myReverse([L1a|L1b],A) :- myReverse(L1b, L2), myAppend(L2,L1a,A).
- myAppend([],X,[X]).
- myAppend([La|Lb],X,A) :- myAppend(Lb,X,A2), A = [La|A2].
- myConcat([],A,A).
- myConcat([L1a|L1b],L2,[L1a|A2]) :- myConcat(L1b,L2,A2).
- myFloor(Input,Output) :- myFloor(Input,Output,0).
- myFloor(Input,Index,Index) :- Index >= Input, Output is Index.
- myFloor(Input,Output,Index) :- Index < Input, Index2 is Index+1, myFloor(Input,Output,Index2).
- % Question X3: SumPicker %
- % see above, Question 5 %
- % Question X4: CountEvens %
- countEvens([],0).
- countEvens([L1a|L1b],Ans) :- is_list(L1a), countEvens(L1a,Ans2), countEvens(L1b,Ans3), Ans is Ans2+Ans3.
- countEvens([L1a|L1b],Ans) :- \+is_list(L1a), isEven(L1a,Even), countEvens(L1b,Ans2), Ans is Ans2+Even.
Advertisement
Add Comment
Please, Sign In to add comment