I need to do this homework assignment using prolog (SWI-flavor) and cant get my head around some things.
For example, if i want to iterate through a list and add it
Check the following programming pattern, which is used quite a lot in Prolog:
You have to either use the cut (!) to forbid backtracking or explicitly check that the condition do not apply in the latter clause.
Note that you said that you wanted to have an output list with the items for which 'something' applied (which is not what you wrote in your code)...
Applying this pattern to your problem it would look something like this:
myRecursion([], []). % This is the base case
myRecursion([Item|Tail], [Item|NTail]):-
something_applies(...),
do_something(...),
only_do_this_if_something(...),
always_do_this(...).
myRecursion(Tail, NTail).
myRecursion([Item|Tail], NTail):-
not(something_applies(...)),
do_something(...),
always_do_this(...),
myRecursion(Tail, NTail).