you should write out how do you want to call this predicate with some sample data, and what you're expecting to get back from it. this is important. :)
okay, I'll do it: teilliste_grK([1,4,2,5,3,6],3,X)
should succeed, instantiating X
to [4,5,6]
, correct? Now try to see which of the clauses does it match with.
So it matches with the second, 1
is indeed =< 3
, and the last line says, continue without the head element (that was smaller than the given K
), and whatever R
we get from there, is our R
as well – R
is R
after all.
Good. So when we come to 4
next, what happens? The 2nd clause matches but is then rejected with 4 =< 2
. Good. On to the third clause.
Again it matches, and the inequality holds, but then you do something strange. You first say that C
is one element longer than R
, and that C
you get from the shorter list - the tail Ta
of your input. Whatever you set your R
in the final clause (the first one), this just can't be.
You want your C
to be shorter than R
, which is to start with the same head Ha
which has just passed the test, so the arguments just need to be put in a different order:
append([Ha], C, R),
(you can write this shorter and simpler though, without any call to append
– what is it?).
Now what about that final (3rd, i.e. []
) clause? If you call teilliste_grK([],3,X)
, what should X
be?