Prolog: How to find a specific element in a list? -


i have implement predicate:

predicate(+connections, +[index,position], -leftconnections); 

the connections variable list looks this:

(conn(symbol1, element11, element21), conn(symbol2, element12, element22), etc) 

i have find connection has symbol equal index variable. then, has verify of 2 elem equal position variable, , retain other (let's call new_pos); next, remove connection found , search of other connections has in of 2 elements, new_pos, , retain new symbol found.

example:

predicate([conn(1,p1,p2), conn(4,p2,p3), conn(3,p3,p1), conn(2,p6,p7)], [1, p2], left);  index = 1, position = p2 => new_position = p1 => new_symbol = 3 leftconnections = [conn(4,p2,p3), conn(3,p3,p1), conn(2,p6,p7)] 

code appreciated!

like in every programming language, must break down steps. first step is, in own words, "find connection has symbol equal index variable."

this quite simple:

predicate(connections, [index,position], remaining) :-     select(conn(index, position, _), connections, remaining). predicate(connections, [index, position2], remaining) :-     select(conn(index, _, position2), connections, remaining). 

both of have anonymous variable new_pos want retain, let's keep it:

predicate(connections, [index, position], remaining) :-     select(conn(index, position, newpos), connections, remaining). predicate(connections, [index, position2], remaining) :-     select(conn(index, newpos, position2), connections, remaining). 

another approach:

predicate(connections, [index, position], remaining) :-     select(conn(index, position, newpos), connections, remaining) ;     select(conn(index, newpos, position), connections, remaining). 

in fact perfect, need somewhere pass newpos out from, or prolog never show unified to.

select_position(connections, [index,pos], remaining, newpos) :-     select(conn(index, pos, newpos), connections, remaining) ;     select(conn(index, newpos, pos), connections, remaining). 

your next step find connection. isn't hard either:

find_with_position(position, connections, connection) :-     member(connection, connections),     (connection = conn(_, position, _) ;      connection = conn(_, _, position)). 

both of these have special-case logic handle fact either item right one. may want consider either using lists can use member.

gluing these isn't hard:

predicate(connections, [index, position], remaining, target) :-     select_position(connections, [index, position], remaining, newpos),     find_with_position(newpos, remaining, conn(target,_,_)). 

try it:

?- predicate([conn(1,p1,p2), conn(4,p2,p3),                conn(3,p3,p1), conn(2,p6,p7)], [1, p2], left, t). left = [conn(4, p2, p3), conn(3, p3, p1), conn(2, p6, p7)], t = 3 ; false. 

you have lot of problems here. if you're learning prolog class, encourage take tutorial, such learn prolog now or amzi adventure.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -