For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2004 > it seems easy...









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 it seems easy...
gai luron

2004-05-16, 11:31 am

.... but I can't write my predicate.

%% box operator
:- op(720,xfy,box).
%% list box operators of the first list in the second one
list_mod_ops([],[]) :- !.
list_mod_ops([R box _ | Cdr],[R|Boxes]) :-
\+ member(R,Boxes),
list_mod_ops(Cdr,Boxes),!.
list_mod_ops([_|Cdr],Boxes) :-
list_mod_ops(Cdr,Boxes).

Suppose I have a list of "things", I am interesting in things matching
with (R box A) and I want to list all the appearing R. I'm not sure it
is well explained.

I would like :
list_mod_ops([a,b,c],[]) -> true
list_mod_ops([2 box e, 7 box b, j, 2 box y], [2,7]) -> true

Does somebody can help me?
Alex Denisov

2004-05-16, 12:31 pm

If I understood you right...

% box operator
:- op(720,xfy,box).
%% list box operators of the first list in the second one

list_mod_ops([],[]).

list_mod_ops([R box _ | Cdr],Boxes) :-
list_mod_ops(Cdr, Boxes1),
(member(R,Boxes1),
Boxes = Boxes1;
Boxes = [R|Boxes1]
).

list_mod_ops([_|Cdr],Boxes) :-
list_mod_ops(Cdr,Boxes).


On Sun, 16 May 2004 16:26:12 +0200, gai luron <gai_luron@oplacha.novalid>
wrote:

> ... but I can't write my predicate.
>
> %% box operator
> :- op(720,xfy,box).
> %% list box operators of the first list in the second one
> list_mod_ops([],[]) :- !.
> list_mod_ops([R box _ | Cdr],[R|Boxes]) :-
> \+ member(R,Boxes),
> list_mod_ops(Cdr,Boxes),!.
> list_mod_ops([_|Cdr],Boxes) :-
> list_mod_ops(Cdr,Boxes).
>
> Suppose I have a list of "things", I am interesting in things matching
> with (R box A) and I want to list all the appearing R. I'm not sure it
> is well explained.
>
> I would like :
> list_mod_ops([a,b,c],[]) -> true
> list_mod_ops([2 box e, 7 box b, j, 2 box y], [2,7]) -> true
>
> Does somebody can help me?


gai luron

2004-05-16, 12:31 pm

Alex Denisov wrote:

> If I understood you right...
>
> % box operator
> :- op(720,xfy,box).
> %% list box operators of the first list in the second one
>
> list_mod_ops([],[]).
>
> list_mod_ops([R box _ | Cdr],Boxes) :-
> list_mod_ops(Cdr, Boxes1),
> (member(R,Boxes1),
> Boxes = Boxes1;
> Boxes = [R|Boxes1]
> ).
>
> list_mod_ops([_|Cdr],Boxes) :-
> list_mod_ops(Cdr,Boxes).


Great!!!
And in fact the important thing is to do the recursive call before
testing if R is member of Boxes. I have done that with my clauses and it
works well.
Thank you Alex.

--
nicolas
Sponsored Links







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

Copyright 2008 codecomments.com