Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 2nd, 2002, 05:33 AM
gilly_uk gilly_uk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 gilly_uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry PROLOG lists

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

Reply With Quote
  #2  
Old December 2nd, 2002, 06:42 AM
Nautilus Nautilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Newcastle, UK
Posts: 6 Nautilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Nautilus
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
----------

Reply With Quote
  #3  
Old December 3rd, 2002, 09:33 AM
Nautilus Nautilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Newcastle, UK
Posts: 6 Nautilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Nautilus
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
----------

Reply With Quote
  #4  
Old December 3rd, 2002, 09:47 AM
gilly_uk gilly_uk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 gilly_uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks

cheers mate you have truely helped me out i know understand how it works. If i pass its down to you.

Reply With Quote
  #5  
Old May 14th, 2005, 06:44 AM
nidcker nidcker is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 nidcker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 48 sec
Reputation Power: 0
Exclamation urgent help

Hi, Im studing in Spain, and ive a problem about the last exercice ive to do in prolog. I need help 'cos its 4 or 5 days remaining to finish it, and ive 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.

Reply With Quote
  #6  
Old May 14th, 2005, 06:45 AM
nidcker nidcker is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 nidcker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 48 sec
Reputation Power: 0
Exclamation urgent help

Hi, Im studing in Spain, and ive a problem about the last exercice ive to do in prolog. I need help 'cos its 4 or 5 days remaining to finish it, and ive 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.

Reply With Quote
  #7  
Old January 14th, 2009, 06:19 PM
A.rachne A.rachne is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 1 A.rachne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 49 sec
Reputation Power: 0
Exclamation Prolog Bubble Sort Help Pleasee

Hi i have a prolog assignment and i have the following question anyone can help me out with this one please?

a. bubble(N, List, NewList) which binds NewList with the List having its Nth element brought to the front.

e.g. bubble(3,[a, b, c, d, e],Ans) binds Ans to [c, a, b, d, e]


Help will be very very appreciated thanks a lot

Reply With Quote
  #8  
Old July 30th, 2011, 08:27 AM
tsoreq tsoreq is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 1 tsoreq User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 27 sec
Reputation Power: 0
need advise with long_conc predicate.

Hi.

i need to write a predicate named long_conc.

input for long_conc is any number of lists (e.g. [1,2,3],[a,b],[32],...,[ab,cd,ef,g])

the predicate should concatanate all lists to a 1 final list.

the answer should not use the predicate conc.

10x!

Reply With Quote
  #9  
Old September 14th, 2011, 09:48 AM
jcsites jcsites is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 1 jcsites User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 28 sec
Reputation Power: 0
Thanks for refreshing my memory. It's been a long time since I had my hands using Prolog.

Reply With Quote
  #10  
Old January 29th, 2012, 09:50 AM
difedo1 difedo1 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 1 difedo1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 12 sec
Reputation Power: 0
Same Problem

Hi Elena, I have the same problem, how you solve those problem??
can you give me some help?

Thank You

Best Regards.


Quote:
Originally Posted by nidcker
Hi, Im studing in Spain, and ive a problem about the last exercice ive to do in prolog. I need help 'cos its 4 or 5 days remaining to finish it, and ive 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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > PROLOG lists


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap