writelist([]). writelist([ Head | Tail ]) :- write(' - '), write(Head), nl, writelist(Tail). concat([], List, List). % Stopping condition - The 2nd list and the result are one and the same % Add the head of the first lsit onto the result concat([ Head | Tail ], List2, [ Head | Result ]) :- concat(Tail, List2, Result). % Recursively add the next item in the first list to the result countlist([ LastItem | [] ], LastItem). countlist([ Head | Rest ], Result) :- countlist(Rest, RecursiveResult), Result is RecursiveResult + Head. listlength([ _ | [] ], 1). listlength([ _ | Tail ], Result) :- listlength(Tail, RecursiveResult), Result is RecursiveResult + 1. loves(cat, milk). loves(dog, bone). loves(mouse, cheese). loves(cat, cheese). % ?- findall(Who, loves(Who, cheese), Result), writelist(Result). score(ben, 45). score(john, 30). score(bob, 25). score(jim, 34). averageScore(Average) :- findall(What, score(_, What), ScoreList), countlist(ScoreList, Count), listlength(ScoreList, Length), Average is Count / Length. isa(polly, parrot). isa(parrot, bird). isa(bird, living_thing). hasa(parrot, wings(2)). hasa(polly, perch). hasa(polly, colour(green)). % ?- findall(green_birds(Who), hasa(Who, colour(green)), Result).