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
Junior Member
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
Junior Member
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
Junior Member
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
Junior Member
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, 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.

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, 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.

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
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > PROLOG lists


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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT