So I'm very new to prolog and have to write a predicate that is satisfiable when an integer list D is the list of prefix sums of a list A.
sums(A, D)
So for example,
sums([4,11,1,-3,8], [4,15,16,13,21]) is satisfiable
I have written this predicate over a dozen different ways to no avail. This is what I currently have written.
sums([], []).
sums([A], [A]).
sums([A|[B|C]], [A|[E|F]]) :- TOTAL is A + B, E = TOTAL, sums([C], [F]).
This somewhat works, in that it will check that the first values of each list are equal, and also check that the second element in the list is correct in that it should be 15. I understand why it works incorrectly in this way, but I am having trouble coming up with how to write it differently, in the correct way.
I have since changed the code to,
sumrunner(L, S) :- sumrunner(L, S, 0).
sumrunner([], [], _).
sumrunner([A], [A], _).
sumrunner([A|B], [C|D], TOTAL) :- TOTAL is TOTAL + A, TOTAL = C,sumrunner(B, D, TOTAL).
However, now it just says false for all cases except for when the two lists are empty, and when the lists both contain one element and they are both equal to each other.
foo(a, b), thenfoois a term referred to as a functor with two argumentsaandb. functor just refers to thefoopart, which must be an atom. But it has no other assigned purpose as a functor. A predicate uses a functor as its head to implement a rule. A functor can also be used to declare facts or otherwise structure data. There are a few different Prolog glossaries online. - lurker