0
votes

I'm stuck on this issue with lists in SWI-prolog. In Prolog a variable can be written once, so i can't deal with this problem.

check(Parameter, [H | T], Result) :- 
   get_res(Parameter, H, Res), 
   check(Res, T, Result).

So, predicate check/3 takes a parameter, a list and gives me the final result. get_res/3 gives me a middle result i use as an input for the recursive case of check/3. So in Result i must have Res for each recursive call. I tried to use append([Parameter], [], Result) before the recursive call but the first time succeeds, then it fails because Result can't be rewritten. I know i also need a base case, it may could be check(_, [], []). But I'm not even sure on that

1

1 Answers

0
votes

For recursively appending your results to the output, you can modify the code as :

check(_,[],[]).
check(Parameter, [H | T], [Res|Result]) :- 
   get_res(Parameter, H, Res), 
   check(Res, T, Result).

Base case is also important in this situation to have a correctly working function. When you are calling check/3, make sure the argument for the Result is a variable s.t. this can work correctly.