teleias

LISP Full Fall2014

Dec 27th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 6.06 KB | None | 0 0
  1. %   Question 1    %
  2. % Strange Average %
  3. %% implement a rule that takes two positive integers
  4. %%  that are each between 0 and 100 (assuming inclusive),
  5. %%  and computes their strangeAverage.
  6. %% the strangeAverage is the average between 0, 100, and the two inputs
  7. strangeAverage(X,Y,S_Average) :-
  8.  X >= 0, X =< 100, Y >= 0, Y =< 100,
  9.  S_Average is (X+Y+100)/4.
  10.  
  11. %  Question 2  %
  12. %   Countdown  %
  13. %% implement a rule that takes a positive integer,
  14. %%  and produces a list that starts at that number and
  15. %%  counts down to 1.
  16. countdown(1,[1]).
  17. countdown(X,[X|L2]) :- X > 1, Y is X-1, countdown(Y, L2).
  18.  
  19. %  Question 3 %
  20. %   Related   %
  21. %% implement a rule that returns true if prolog can prove
  22. %%  that the first person is related to the second person.
  23. %% note: there is some extraneous code here because I
  24. %%        wasn't sure of the definition of related.
  25. %%        the extra code allows flexibility if
  26. %%        my original premise was incorrect.
  27. %% related if recursively [hRelated or vRelated]
  28. %%  hRelated if siblings and not self
  29. %%   sibling if they share a parent
  30. %%  vRelated if they are recursively [parent or child]
  31. % helper functions:
  32. %  sibling isRecursiveChildOf isRecursiveParentOf vRelated hRelated
  33. %%% facts %%%
  34. parent(fred, mary).
  35. parent(mary, jim).
  36. parent(mary, greg).
  37. parent(angie, terry).
  38. parent(terry, marge).
  39. %%% rules %%%
  40. sibling(A,B) :- parent(C,A), parent(C,B).
  41. isRecursiveChildOf(P,Cx) :- parent(P, Cx).
  42. isRecursiveChildOf(P,Cx) :- parent(P, C), isRecursiveChildOf(C,Cx).
  43. isRecursiveParentOf(C,P) :- isRecursiveChildOf(P,C).
  44. vRelated(A,B) :- isRecursiveParentOf(A,B).
  45. vRelated(A,B) :- isRecursiveChildOf(A,B).
  46. hRelated(A,B) :- sibling(A,B), A \= B.
  47. related(A,B)  :- hRelated(A,B).
  48. related(A,B)  :- vRelated(A,B).
  49.  
  50. %  Question 4  %
  51. %    Sphinx    %
  52. %% solves the cryptarithmetic problem
  53. %%  called the "sphinx"
  54. %% general direction was taken from the following resource:
  55. %%  http://clip.dia.fi.upm.es/~vocal/public_info/seminar_notes/node13.html
  56. %%  I worked on this problem before it was discussed in class, and
  57. %%  searching google for "generate and test" brought this up. sorry.
  58. % helper functions:
  59. %  assignAll assign numMaker
  60. assignAll([],_).
  61. assignAll([L1a|L1b], Digits) :- assign(L1a, Digits, Digits2), assignAll(L1b, Digits2).
  62. assign(X, [X|L], L).
  63. assign(X, [L1a|L1b], [L1a|L2b]) :- assign(X, L1b, L2b).
  64. sphinx(S,E,P,T,M,B,R) :-
  65. assignAll([S,E,P,T,M,B,R], [0,1,2,3,4,5,6,7,8,9]),
  66. S \= 0, E \= 0, M \= 0,
  67. numMaker([S,E,P,T,E,M,B,R,E],X1),
  68. numMaker([E,R,B,M,E,T,P,E,S],X2),
  69. numMaker([M,P,P,B,R,P,S,S,M],X3),
  70. X1 - X2 =:= X3.
  71. numMaker([L1a|L1b],Answer) :- numMaker([L1a|L1b],Iterations,Answer), Iterations > 0.
  72. numMaker([],Iterations,Answer) :- Iterations is -1, Answer is 0.
  73. numMaker([L1a|L1b],Iterations,Answer) :- numMaker(L1b,Iterations2,Answer2),
  74.  Iterations is Iterations2+1, Answer is Answer2+(L1a*(10^Iterations)).
  75.  
  76. %  Question 5  %
  77. %   SumPicker  %
  78. %% takes two lists. the first list is the picker,
  79. %%  and the second list is the data.
  80. %%  this program sums up the numbers in the data list
  81. %%  referenced by the picker list.
  82. % helper functions:
  83. %  contains
  84. contains(_, [], Count) :- Count is 0.
  85. contains(X, [X|L2], Count) :- contains(X, L2, C), Count is C+1.
  86. contains(X, [L1|L2], Count) :- X \= L1, contains(X, L2, Count).
  87. sumPicker(L1,L2,A)   :- sumPicker(1,L1,L2,A).
  88. sumPicker(_,_,[],A) :- A is 0.
  89. sumPicker(X,L1,[_|L2b],A) :- contains(X,L1,C), C <  1, Y is X+1, sumPicker(Y,L1,L2b,A2), A is A2.
  90. 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.
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. % Question X1: Fracktorial %
  105. fracktorial(X, Ans) :- isEven(X, Even), fracktorial(X, Ans, Even).
  106. fracktorial(0, Ans, _) :- Ans is 1.
  107. fracktorial(X, Ans, 1) :- X > 0, Y is X-2, fracktorial(Y, Ans2, 1), Ans is Ans2*X.
  108. fracktorial(X, Ans, 0) :- X > 0, Y is X-1, fracktorial(Y, Ans2, 1), Ans is Ans2.
  109. isEven(0, Ans) :- Ans is 1.
  110. isEven(1, Ans) :- Ans is 0.
  111. isEven(X, Ans) :- X > 1, Y is X-2, isEven(Y, Ans).
  112.  
  113.  
  114. % Question X2: ReverseListHalves %
  115. reverseListHalves(L, A) :- count(L, Count), isEven(Count, Even), reverseListHalves(L,A,Even).
  116. reverseListHalves(L, A, 0) :- %if odd
  117.  count(L, Count),
  118.  End is Count-1, myFloor(End/2,Middle), Start is 0,
  119.  MiddlePre is Middle-1, MiddlePost is Middle+1,
  120.  sub(Start,MiddlePre,L,Half1), sub(MiddlePost,End,L,Half2),
  121.  myReverse(Half1,Half1r), myReverse(Half2,Half2r),
  122.  getIndexInL(Middle,L,MiddleElement),
  123.  myAppend(Half1r,MiddleElement,Half1r2),
  124.  myConcat(Half1r2,Half2r,A).
  125. reverseListHalves(L, A, 1) :- %if even
  126.  count(L, Count),
  127.  End is Count-1, Middle is End/2, Start is 0,
  128.  sub(Start,Middle,L,Half1), sub(Middle,End,L,Half2),
  129.  myReverse(Half1,Half1r), myReverse(Half2,Half2r),
  130.  myConcat(Half1r,Half2r,A).
  131. count([], A) :- A is 0.
  132. count([_|L1b], A) :- count(L1b, A2), A is A2+1.
  133. sub(Start,End,L,A) :- sub(0,Start,End,L,A).
  134. sub(_,_,_,[],A).
  135. sub(Index,_,End,L,A) :- Index > End, A = [].
  136. sub(Index,Start,End,L,A) :- Index < Start, Index2 is Index+1, sub(Index2,Start,End,L,A).
  137. 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).
  138. getIndexInL(Index,L,A) :- getIndexInL(Index,0,L,A).
  139. getIndexInL(Index,Index,[A|_],A).
  140. getIndexInL(Index,X,[L1a|L1b],A) :- Index \= X, X2 is X+1, getIndexInL(Index,X2,L1b,A).
  141. myReverse([],L2) :- L2 = [].
  142. myReverse([L1a|L1b],A) :- myReverse(L1b, L2), myAppend(L2,L1a,A).
  143. myAppend([],X,[X]).
  144. myAppend([La|Lb],X,A) :- myAppend(Lb,X,A2), A = [La|A2].
  145. myConcat([],A,A).
  146. myConcat([L1a|L1b],L2,[L1a|A2]) :- myConcat(L1b,L2,A2).
  147. myFloor(Input,Output) :- myFloor(Input,Output,0).
  148. myFloor(Input,Index,Index) :- Index >= Input, Output is Index.
  149. myFloor(Input,Output,Index) :- Index < Input, Index2 is Index+1, myFloor(Input,Output,Index2).
  150.  
  151. % Question X3: SumPicker %
  152. %  see above, Question 5 %
  153.  
  154. % Question X4: CountEvens %
  155. countEvens([],0).
  156. countEvens([L1a|L1b],Ans) :-   is_list(L1a), countEvens(L1a,Ans2), countEvens(L1b,Ans3), Ans is Ans2+Ans3.
  157. 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