|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Im revising for an exam next week and i cant get my head round this exercise in the notes i have. Its asks to use the method call
move(PermutationList,NewPermutationList). Where given a permutation list as input, will produce as output a list identical to the first but in which two elements have been swapped. Through backtracking the procedure should produce all possible swaps. e.g move([2,1],SL). SL = [1,2] move([1,2,3],SL). SL = [2,1,3] SL = [3,2,1] SL = [1,3,2] I have written some code which works for [2,1] but thats it. move([],[]). move([H,T|Rest],[T,H|Rest]):- move(Rest,Rest). please help me |
|
#2
|
|||
|
|||
|
I'm no expert in Prolog (i'm learning it for an exam too) and I'm not currently at a PC which runs prolog so I can't test this, also remember that there are many prolog versions out there and all can act differently, but you could try something like -
swap([],[]). % swap first two elements swap([X,Y|Rest], [Y,X|Rest1]) :- swap(X, Y). % otherwise swap elements in tail swap([Z|Rest], [Z|Rest1]) :- swap(Rest, Rest1). The second statement allows a loop, going back to the first swap but with the remaining list and not the first element so swap([1,2,3],SL). SHOULD give you: SL = [2,1,3] SL = [2,3,1] SL = [3,2,1] SL = [3,1,2] SL = [1,3,2] ... and back to... SL = [1,2,3] I could test this in the next few hours for you and get back to whether I'm talking complete mince. Nautilus ---------- |
|
#3
|
|||
|
|||
|
I tested this last night and, indeed, I WAS talking slight mince.
this new solution - from memory, so it may be wrong again - will swap the first two elements and then trawl through the list and swap two ADJACENT elements in the tail. % swap empty list swap([],[]). % swap first two elements of list swap([X,Y|Rest], [Y,X|Rest1]). % then loop tail of list back into swap rule swap([Z|Rest], [Z|Rest1]) :- swap(Rest, Rest1). when tested it shows: swap([1,2,3],SL). SL = [2,1,3] ? ; % swaps first two SL = [1,3,2] ? ; % swaps last two (first two of tail) SL = [1,2,3] yes swap([1,2,3,4],SL). SL = [2,1,3,4] ? ; % swaps first two SL = [1,3,2,4] ? ; % swaps middle two (first two of tail) SL = [1,2,4,3] ? ; % swaps last two (first two of new tail) SL = [1,2,3,4] yes swap([1,2,3,4,5],SL). SL = [2,1,3,4,5] ? ; % swaps first two SL = [1,3,2,4,5] ? ; % swaps next two SL = [1,2,4,3,5] ? ; % swaps next two SL = [1,2,3,5,4] ? ; % swaps last two SL = [1,2,3,4,5] yes I'm not sure how to swap ANY two elements in a list (first and last, first and 4th, etc) - try searching google for 'prolog list sort' and that sort of thing, you might find something that helps. try: swap([X,Y|Rest], [Y,Rest|X]). % put first element at end although I have no idea if that will work Hope i was useful, if nothing else you've helped me revise for my exam too. good luck Nautilus ---------- |
|
#4
|
|||
|
|||
|
thanks
cheers mate you have truely helped me out i know understand how it works. If i pass its down to you.
|
|
#5
|
|||
|
|||
|
Hi, I´m studing in Spain, and i´ve a problem about the last exercice i´ve to do in prolog. I need help 'cos its 4 or 5 days remaining to finish it, and i´ve no idea how to do it
![]() The problem is : --------------------------------------------------------------- Construct a program to draw a figure (graph) using a continuous line, without passing twice through the same edge nor having to lift the pencil off the page. The predicate will be draw(GraphName, Path) where "GraphName" is the name assigned to one graph representation (_not_ the graph representation) and "Path" is an ordered list of the nodes that have to be traversed. The possible input graphs to the program will be stored using facts in the predicate graph/2, in this way graph(GraphName, Graph) <- . a / \ b---c For example, given the graph |\ /| | X | |/ \| d---e (X is not a node, it is an edge crossing), its representation would be graph(house, [l(a,b),l(a,c),l(b,c),l(b,d),l(b,e),l(c,d),l(c,e),l (d,e)]) <- . and the program execution would give the following: ?- draw(house, Path). Path = [d,b,a,c,b,e,c,d,e] ? <return> yes b /|\ The graph a-+-d represented by \|/ c graph(diamond, [l(a,b),l(a,c),l(b,c),l(a,d),l(b,d),l(c,d)]) <- . would make the program behave in this way: ?- draw(diamond, Traversal). no since there is no path that respects the proposed conditions. ---------------------------------------------------------------- Thank you so much for your time and help. Elena. |
|
#6
|
|||
|
|||
|
Hi, I´m studing in Spain, and i´ve a problem about the last exercice i´ve to do in prolog. I need help 'cos its 4 or 5 days remaining to finish it, and i´ve no idea how to do it
![]() The problem is : --------------------------------------------------------------- Construct a program to draw a figure (graph) using a continuous line, without passing twice through the same edge nor having to lift the pencil off the page. The predicate will be draw(GraphName, Path) where "GraphName" is the name assigned to one graph representation (_not_ the graph representation) and "Path" is an ordered list of the nodes that have to be traversed. The possible input graphs to the program will be stored using facts in the predicate graph/2, in this way graph(GraphName, Graph) <- . For example, given the graph a / \ b---c |\ /| | X | |/ \| d---e (X is not a node, it is an edge crossing), its representation would be graph(house, [l(a,b),l(a,c),l(b,c),l(b,d),l(b,e),l(c,d),l(c,e),l (d,e)]) <- . and the program execution would give the following: ?- draw(house, Path). Path = [d,b,a,c,b,e,c,d,e] ? <return> yes The graph represented by b /|\ a-+-d \|/ c graph(diamond, [l(a,b),l(a,c),l(b,c),l(a,d),l(b,d),l(c,d)]) <- . would make the program behave in this way: ?- draw(diamond, Traversal). no since there is no path that respects the proposed conditions. ---------------------------------------------------------------- Thank you so much for your time and help. Elena. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Programming Tools > PROLOG lists |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|