Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

permuting lists
I'm a prolog newbie and I'm trying to get some practice. What I would
like to do is create a rule that will permute a list. I'm trying to
get something to work sort of like this:

car_row(X) :- permute([red,blue,green,yellow],H),
permute([john,bill,mary,sue], P), join(H,P,X).

X would then be a list that might contain:
[car(yellow,bill), car(red,sue), car(blue,mary), car(green, john)]

I'm trying to use the select rule to make the first element of the
permuted list any element of the original list with the rest being a
permutation of the remaining elements of the original list, but so far
I'm having no luck. I would think that I could do this by defining
permute to recursively permute the remaining elements in the list sort
of like this:

permute([],[]).
permute(X,H) :- select(A,X,Rest), <Put A in H>, <select/permute from
Rest>.

I am  as to how I get H to 'fill up' with all of the values
that were selected.

The join rule above will just be used to join the 2 lists together (I
can handle that ;) ).

Any help is appreciated!

Report this thread to moderator Post Follow-up to this message
Old Post
dirty_bit
12-07-04 09:13 AM


Re: permuting lists
How about :

permute([],[]).
permute(X,[A|H]) :- select(A,X,Rest), permute(Rest, H).


"dirty_bit" <dod_xenozhar@hotmail.com> wrote in message
news:4d0ff9ea.0412051737.6a2fc468@posting.google.com...
> I'm a prolog newbie and I'm trying to get some practice. What I would
> like to do is create a rule that will permute a list. I'm trying to
> get something to work sort of like this:
>
> car_row(X) :- permute([red,blue,green,yellow],H),
>                permute([john,bill,mary,sue], P), join(H,P,X).
>
> X would then be a list that might contain:
> [car(yellow,bill), car(red,sue), car(blue,mary), car(green, john)]
>
> I'm trying to use the select rule to make the first element of the
> permuted list any element of the original list with the rest being a
> permutation of the remaining elements of the original list, but so far
> I'm having no luck. I would think that I could do this by defining
> permute to recursively permute the remaining elements in the list sort
> of like this:
>
> permute([],[]).
> permute(X,H) :- select(A,X,Rest), <Put A in H>, <select/permute from
> Rest>.
>
> I am  as to how I get H to 'fill up' with all of the values
> that were selected.
>
> The join rule above will just be used to join the 2 lists together (I
> can handle that ;) ).
>
> Any help is appreciated!



Report this thread to moderator Post Follow-up to this message
Old Post
Benjamin Johnston
12-07-04 02:05 PM


Re: permuting lists
dirty_bit wrote:
> I'm a prolog newbie and I'm trying to get some practice. What I would
> like to do is create a rule that will permute a list. I'm trying to
> get something to work sort of like this:
>
> car_row(X) :- permute([red,blue,green,yellow],H),
>                 permute([john,bill,mary,sue], P), join(H,P,X).
>
> X would then be a list that might contain:
> [car(yellow,bill), car(red,sue), car(blue,mary), car(green, john)]
>

I think this is a very good first draft.

But a rule is merely a declarative sentence and, as such, doesn't
"do" anything.

Given a list of rules and a question, the Prolog Inference Engine
will find all of the answers that the given list of rules supplies to
the given question.

For the PIE to derive any answers from the rule

> car_row(X) :- permute([red,blue,green,yellow],H),
>                 permute([john,bill,mary,sue], P), join(H,P,X).

it will have to know what you mean by 'permute/2' and 'join/3'.


> I'm trying to use the select rule to make the first element of the
> permuted list any element of the original list with the rest being a
> permutation of the remaining elements of the original list, but so far
> I'm having no luck. I would think that I could do this by defining
> permute to recursively permute the remaining elements in the list sort
> of like this:
>
> permute([],[]).
> permute(X,H) :- select(A,X,Rest), <Put A in H>, <select/permute from
> Rest>.
>
> I am  as to how I get H to 'fill up' with all of the values
> that were selected.
>

permute([],[]).

permute(X,H) :-
select(A,X,Rest),
H=[A|Something],
<select/permute Something from Rest>.  ?


Try it!


--
billh


Report this thread to moderator Post Follow-up to this message
Old Post
student
12-09-04 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:29 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.