score(Movies,Total) :-
findall(Money,(member(Movie,Movies),takings(Movie,Money)),Profit),
sum_list(Profit,Total)
I want to convert this rule using recursion but I am not sure how. Help! Link to the previous question that answers this one : Sum up data from facts
Example run
Explanation of code
List in Prolog are recursively constructed and deconstructed using the | operator. As 0 is the initial value for counting integers, [ ], known as the empty list, is the initial value for a closed list. To recursively build to the front of a closed list one uses the current list and the | operator. When using | in Prolog it is common to refer to the item being added as the Head, (H) and the current closed list being added to as the Tail, (T), and this why you often see the notation [H|T] when talking about list in Prolog.
So to construct the list [1,2,3] would be [] then add 1, [1].
To add 2 would be [1] then add 2, [2,1].
To deconstruct the list [1,2,3] would use variables so [H|T] will become H being 1 with T being [2,3].
When you get to the end of the list it is just [] with out a head.
So in the code
[Movie|Movies]takes input list of movies and unifies the head of the listMoviewith the first movie and passes the tail of the listMoviesrecursively to the predicate. Only when the list is empty will the clausescore([],0)match and unify the 0 withTotal0inscore(Movies,Total0).I know this can have a lot more detail in explanations, but these explanations are all over the place in Prolog examples.