Newbie: Want comments or/and proposol for a better solution to a little problem ...
From: pekka (pekka_at_koping.net)
Date: 02/16/04
- Next message: djamé: "Re: Help with project"
- Previous message: Andrzej Lewandowski: "Re: Help with project"
- Next in thread: Tom Breton: "Re: Newbie: Want comments or/and proposol for a better solution to a little problem ..."
- Reply: Tom Breton: "Re: Newbie: Want comments or/and proposol for a better solution to a little problem ..."
- Reply: Tom Breton: "Re: Newbie: Want comments or/and proposol for a better solution to a little problem ..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 16 Feb 2004 12:52:09 -0800
Hello,
First I want to say it's interesting to rewad the "discussion" about
Prolog in the thread "Newbie, The secret with Prolog:"... which I
started with me questions;)
Enough about that, now I'm asking for any comments or/and proposol for
a better solution to a little problem, I have been struggled for a
time ...
I have worked hard to get a solution to a "simple" problem (code is
coming afet this text) for any who knows to do;) as always, but now I
have found one solution (seems to work) to find cousins without any
duplicates, i.e. it's enough with cousins [a,b] once, will not have
for example [a,b], [a,b], [b,a] ...
But It feels that my solution is "complicated"? Please give comments
or/and proposol for a better solution. I will probably learn something
or/and get a bigger understanding!
%-- Here starts code
----------------------------------------------------------
/* Below a number of relations are stated. Annakin is parent to Luke
etc.*/
parent(annakin,luke).
parent(carolyn,luke).
parent(annakin,leia).
parent(carolyn,leia).
parent(han,jacen).
parent(leia,jacen).
parent(han,jane).
parent(leia,jane).
parent(luke,brenda).
parent(shannon,brenda).
% Pekka added data below to test Task 3
parent(luke, sandra).
parent(julia, sandra).
/* Task 4:
Create a list with pairs of cousins, e.g. [[aasa, nisse],
[per,berra],..]
Remove doubles, e.g. [nisse, aasa] is not allowed in the above list.
Remember that setof does not identify and remove [nisse, aasa] and
[aasa, nisse].
Define a predicate that answers this question. */
sibling(X,Y) :-
parent(Z,X),
parent(Z,Y),
X \== Y.
cousins(X,Y) :- parent(P1,X),
parent(P2,Y),
X \== Y,
sibling(P1,P2),
P1 \== P2. % => Lots of pairs?
% Predicate below answer Yes if inverse_cousine is in list
check_list_for_inverse_cousine([X,Y],CL) :- member([Y,X],CL).
%setof( [X,Y], cousins(X,Y),L), % gives lots of pairs
get_cousins(Clist) :- setof( [X,Y], cousins(X,Y),Clist).
% Predicate below gives a list with cousins with no duplicates,
% as stated in assignment above
delete_duplicate_cousins_in_list(OutList) :- get_cousins(Clist),
delete_duplicate_cousins(Clist,OutList).
delete_duplicate_cousins([], NL).
delete_duplicate_cousins(L, NL) :- delete_duplicate_cousins(L,[],NL).
delete_duplicate_cousins([],NL,NL).
delete_duplicate_cousins([H|T],TL,NL) :-
( \+member(H,TL), \+check_list_for_inverse_cousine(H,TL) ->
delete_duplicate_cousins(T, [H|TL],NL); % Add to TempList
delete_duplicate_cousins(T,TL,NL)
).
%-- Here ends code ------------------------------------------------------------
// Looking forward for some answers
- Next message: djamé: "Re: Help with project"
- Previous message: Andrzej Lewandowski: "Re: Help with project"
- Next in thread: Tom Breton: "Re: Newbie: Want comments or/and proposol for a better solution to a little problem ..."
- Reply: Tom Breton: "Re: Newbie: Want comments or/and proposol for a better solution to a little problem ..."
- Reply: Tom Breton: "Re: Newbie: Want comments or/and proposol for a better solution to a little problem ..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]