Problem of resourcefulness

Asked

Viewed 72 times

1

How do we fix this? I am trying to make a program that receives a path and translates in directions to follow (up(0),right(90),left(270),low(180)). The predicate vizinha of the Dir one of the directions (0,90,180,270), for example: test([ (5, 5), (5, 6), (6, 6), (6, 7), (7, 7)],L). should give L=[0,90,0,90], but of L=[0]. I made trace and this the link of the image The trace

test([(_,_)],_).

test([A|P],List):-
    B=(_,_),
    append([B],_,P),     
    vizinha(A,B,Dir), 
    append(List,[Dir],List1), 
    test(P,List1).
  • Hello, this is the Portguese Stackoverflow, for questions in English Please, go to the http://stackoverflow.com.

1 answer

1

Your second append is with the arguments in the wrong order. Note that the output value of test is List, then List is that it has to start with Dir and end with the recursion result:

append([Dir], List1, List),

Also, your base case is returning "anything" if the input only has one element - the ideal would be to return the empty list, otherwise there will be infinite results:

test([(_,_)], []).

Example in the ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.