For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2004 > removing duplicates from result - newbee









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 removing duplicates from result - newbee
Antony Sequeira

2004-03-27, 12:10 am

I am learning prolog using the prolog book(1986) by Bratko.
I consult the following code

conc([],L,L).
conc([X|L1],L2,[X|L3]) :-
conc(L1,L2,L3).

sublist(S,L) :-
conc(_,L2,L),
conc(S,_,L2).

I try
sublist(X,[a,b]).
I get the following results.
?- sublist(X,[a,b]).

X = [] ;

X = [a] ;

X = [a, b] ;

X = [] ;

X = [b] ;

X = []

Is it expected behavior to get the same result (the empty list) more
than once?
Is this too early for me to be asking this question(my prolog knowledge
is limited to the first three chapters of Bratko)?
I am using SWI-Prolog version 5.2.11 on Windows 2000.


Thanks,
--
Antony Sequeira
use my full name at hotmail.com

Nick Wedd

2004-03-27, 12:10 am

In message <c32l3g$svr$1@pixie.nscp.aoltw.net>, Antony Sequeira
<usemyfullname@hotmail.com> writes
>I am learning prolog using the prolog book(1986) by Bratko.
>I consult the following code
>
>conc([],L,L).
>conc([X|L1],L2,[X|L3]) :-
> conc(L1,L2,L3).
>
>sublist(S,L) :-
> conc(_,L2,L),
> conc(S,_,L2).
>
>I try
> sublist(X,[a,b]).
>I get the following results.
>?- sublist(X,[a,b]).
>
>X = [] ;
>
>X = [a] ;
>
>X = [a, b] ;
>
>X = [] ;
>
>X = [b] ;
>
>X = []
>
>Is it expected behavior to get the same result (the empty list) more
>than once?


If you define sublist/2 like that, it is the expected outcome.

If you want S to be non-empty, you can modify sublist/2 accordingly. If
you want it to generate the empty list, but only once, you can also
modify sublist/2 accordingly.

>Is this too early for me to be asking this question(my prolog knowledge
>is limited to the first three chapters of Bratko)?
>I am using SWI-Prolog version 5.2.11 on Windows 2000.


It's a sensible question. And the answer does not depend on the Prolog
version. The behaviour you have described is correct.

Nick
--
Nick Wedd nick@maproom.co.uk
Sponsored Links







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

Copyright 2008 codecomments.com