Advertisement
starbeamrainbowlabs

Prolog Lab #7

Nov 16th, 2015
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.29 KB | None | 0 0
  1. writelist([]).
  2. writelist([ Head | Tail ]) :-
  3.     write(' - '), write(Head), nl,
  4.     writelist(Tail).
  5.  
  6. concat([], List, List). % Stopping condition - The 2nd list and the result are one and the same
  7. % Add the head of the first lsit onto the result
  8. concat([ Head | Tail ], List2, [ Head | Result ]) :-
  9.     concat(Tail, List2, Result). % Recursively add the next item in the first list to the result
  10.  
  11. countlist([ LastItem | [] ], LastItem).
  12. countlist([ Head | Rest ], Result) :-
  13.     countlist(Rest, RecursiveResult),
  14.     Result is RecursiveResult + Head.
  15.  
  16. listlength([ _ | [] ], 1).
  17. listlength([ _ | Tail ], Result) :-
  18.     listlength(Tail, RecursiveResult),
  19.     Result is RecursiveResult + 1.
  20.  
  21. loves(cat, milk).
  22. loves(dog, bone).
  23. loves(mouse, cheese).
  24. loves(cat, cheese).
  25.  
  26. % ?- findall(Who, loves(Who, cheese), Result), writelist(Result).
  27.  
  28. score(ben, 45).
  29. score(john, 30).
  30. score(bob, 25).
  31. score(jim, 34).
  32.  
  33. averageScore(Average) :-
  34.     findall(What, score(_, What), ScoreList),
  35.     countlist(ScoreList, Count),
  36.     listlength(ScoreList, Length),
  37.     Average is Count / Length.
  38.  
  39. isa(polly, parrot).
  40. isa(parrot, bird).
  41. isa(bird, living_thing).
  42. hasa(parrot, wings(2)).
  43. hasa(polly, perch).
  44. hasa(polly, colour(green)).
  45.  
  46. % ?- findall(green_birds(Who), hasa(Who, colour(green)), Result).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement