For Programmers: Free Programming Magazines  


Home > Archive > Prolog > December 2004 > permuting lists









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author permuting lists
dirty_bit

2004-12-07, 4:13 am

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!
Benjamin Johnston

2004-12-07, 9:05 am


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!



student

2004-12-08, 8:58 pm

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com