Your task

Define Prolog inference rules called listSum such that a query of the form

listSum(List, Sum).

asserts that the sum of the elements of List is Sum. For example, the query

listSum([8, 0, 18, 15, 5], X).

should result in the query succeeding with the binding of 46 to X.

Hints

Think about an appropriate base case, and define an inference rule to handle the base case.

You can use the is keyword to make an assertion about numeric equality, e.g., the query

X is 4 + 5.

succeeds with the binding of 9 to X.

You can use the [First|Rest] syntax to break up a non-empty list into a first element and remaining elements list.

Note: Prolog is sensitive to the order in which the conjuncts in the hypothesis appear. If your rule doesn’t work, try reordering the clauses in the hypothesis of the recursive rule.