Re: Converting Scheme to Prolog
- From: Daemon <kewldaemon@xxxxxxxxx>
- Date: Sat, 06 Oct 2007 15:58:43 -0000
On Oct 5, 7:51 am, Jan Wielemaker <j...@xxxxxxxxxxxxxxxxxxx> wrote:
On 2007-10-05, Pierpaolo BERNARDI <pierpa...@xxxxxxxxxxxxx> wrote:
On Thu, 04 Oct 2007 23:09:01 +0200, Chip Eastham <hardm...@xxxxxxxxx> wrote:
On Oct 4, 4:25 pm, pineapple.l...@xxxxxxxxx wrote:
You speak of an "islist" test. I am not aware of this. Is this
a built-in predicate? And is this the preferred way of doing this
sort of thing (testing for a list?).
In both Amzi! Prolog and SWI-Prolog there's a built-in predicate
is_list/1, which succeeds either for something that unifies with
[H|T] or with the empty list, [ ]. So there's a little room for
discrepancy.
To be precise, at least in SWI-Prolog, is_list/1 checks that its argument
is a *proper* list.
I have the impression the community wants to phase out the term
*proper* list. A list is [] or [_|<a list>]. In addition we have a
_partial_ list, which is a variable or [_|<partial list>]. [a|a] is
just a term, most likely unintended. Very old versions of SWI-Prolog
had is_list as
is_list(X) :- var(X), !, fail.
is_list([]).
is_list([_|_]).
and proper list doing what is_list is doing now. Recent versions also
check for cycles, so X = [_|X], is_list(X) fails, as it should.
is_list([a|a]) will fail.
The implication of this is that is_list/1 is O(n) for lists of length n.
Not knowing this fact, one can easily transform a simple linear predicate
to a quadratic one. :)
The consequence is indeed that you should not use is_list/1 in loops.
Use it for a proper type test at entry of the predicate and than use
[] and [_|_] to process the list. Good news is that you are garanteed
to terminate :-)
Cheers --- Jan
Ok folks, last problem/question. As I mentioned earlier in my thread,
I wanted to convert a Scheme program that did the following;
(ncombs n L) returns a list containing lists of size n, one for each
combination of size n from the elements of L. Such that
(ncombs 4 '(a b c d e))
would return
((e d c b) (e d c a) (e d b a) (d c b a) (e c b a)).
I finally figured it out with the following;
ncombs(0, _, []).
ncombs(N, [X | L], [X | L1]) :- N > 0, M is N - 1, ncombs(M, L, L1).
ncombs(N, [_ | L], L1) :- N > 0, ncombs(N, L, L1).
ncombs(4, [a,b,c,d,e], L).
returns;
L = [a, b, c, d] ;
L = [a, b, c, e] ;
L = [a, b, d, e] ;
L = [a, c, d, e] ;
L = [b, c, d, e] ;
What I'm trying to figure out is, how can I make it return;
L = [[a, b, c, d][a, b, c, e][a, b, d, e]a, c, d, e][b, c, d, e]]
"and" not be truncated.
Thanks
.
- Follow-Ups:
- Re: Converting Scheme to Prolog
- From: Daemon
- Re: Converting Scheme to Prolog
- References:
- Converting Scheme to Prolog
- From: Daemon
- Re: Converting Scheme to Prolog
- From: Pierpaolo BERNARDI
- Re: Converting Scheme to Prolog
- From: Daemon
- Re: Converting Scheme to Prolog
- From: pineapple . link
- Re: Converting Scheme to Prolog
- From: pineapple . link
- Re: Converting Scheme to Prolog
- From: Daemon
- Re: Converting Scheme to Prolog
- From: Matthew Huntbach
- Re: Converting Scheme to Prolog
- From: pineapple . link
- Re: Converting Scheme to Prolog
- From: Chip Eastham
- Re: Converting Scheme to Prolog
- From: Pierpaolo BERNARDI
- Re: Converting Scheme to Prolog
- From: Jan Wielemaker
- Converting Scheme to Prolog
- Prev by Date: Re: Converting Scheme to Prolog
- Next by Date: Re: Converting Scheme to Prolog
- Previous by thread: Re: Converting Scheme to Prolog
- Next by thread: Re: Converting Scheme to Prolog
- Index(es):
Relevant Pages
|