2

I've written a small Prolog program that's supposed to check a list and see whether the head Ha is greater than K, if it is, it is supposed to append it to a list R and after checking the whole list give back R which then would consist of bigger than K integers.

This code returns R = [].

teilliste_grK([],_,_).

teilliste_grK([Ha|Ta], K, R) :-
    Ha =< K,
    teilliste_grK(Ta,K,R).

teilliste_grK([Ha|Ta], K, R) :-
    Ha > K,
    append(R, [Ha], C),
    teilliste_grK(Ta, K, C).

Any help?

EviL GaMer
  • 133
  • 13

1 Answers1

1

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 wellR 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?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • I tried `append(R, Ha, R)` which is what I want but it wouldn't work. Problem is I'm quite new to Prolog and coming from Java which is mighty different. After the last call `X` should be the whole list which was appended through the recursive calls. – EviL GaMer Apr 18 '15 at 21:08
  • Ok, after implementing `append([Ha], C, R),` it works kinda but gives me an empty tail on the R-list, which is probably the case with my base call and I'll try to trace it. But I don't get the append order, I thought the first argument is the list to which I append the second argument and save it in the last argument. – EviL GaMer Apr 18 '15 at 21:16
  • Got the result I wanted with: `teilliste_grK([],_,R):- append([], R).` Though more through try and error then skill. What would be the simpler method without append? – EviL GaMer Apr 18 '15 at 21:23
  • [`append([], R)`](http://www.swi-prolog.org/pldoc/doc_for?object=append/2) appends together all lists inside its first argument - which is here empty, so the result is empty too: `R = []`. you can just write *that* instead. :) – Will Ness Apr 18 '15 at 21:33
  • You said I could write this `append([Ha], C, R),` shorter and without append, how? And thanks for the help by the way. While Prolog is simpler that's what making it difficult for me :) – EviL GaMer Apr 18 '15 at 21:40
  • about appending, see if [this answer](http://stackoverflow.com/a/11551001/849891) clarifies things. (disclaimer: that answer is by me also). – Will Ness Apr 18 '15 at 21:44