Re: kombinations
- From: Nick Wedd <nick@xxxxxxxxxxxxx>
- Date: Sun, 10 Feb 2008 11:17:40 +0000
In message <fome1k$q2u$1@xxxxxxxxxxxxxxxxxxxxxxxxx>, Stephan Lukits <Stephan.Lukits@xxxxxxxxxxxxxxxx> writes
Hi,
I'd like to have all lists with the length N whereas its
elements can be numbers between 1 and 6 (including).
I tried the following code:
check1_6(X) :-
X is 1; X is 2; X is 3; X is 4; X is 5; X is 6.
komb6(1, [E]) :-
check1_6(E).
komb6(N, [H|T]) :-
check1_6(H),
Next is N - 1,
komb6(Next, T).
which gives me the proper first six lists followed by
a local stack overflow:
?- komb6(3, L).
L = [1, 1, 1] ;
L = [1, 1, 2] ;
L = [1, 1, 3] ;
L = [1, 1, 4] ;
L = [1, 1, 5] ;
L = [1, 1, 6] ;
ERROR: Out of local stack
What I'm doing wrong?
(I'm using an up to date swi-prolog on a linux box with 2.6 kernel.)
Thanks for any help.
You have two clauses that match komb6(1, T). After it's tried the first one, it's going to try the second one, and you don't want it to.
Nick
P.S.
Nothing to do with your problem, but I would have written check1_6 as
check1_6( X ) :- member( X, [1,2,3,4,5,6] ).
or as
check1_6( 1 ).
check1_6( 2 ).
check1_6( 3 ).
check1_6( 4 ).
check1_6( 5 ).
check1_6( 6 ).
Nick
--
Nick Wedd nick@xxxxxxxxxxxxx
.
- Follow-Ups:
- [solved] Re: kombinations
- From: Stephan Lukits
- [solved] Re: kombinations
- References:
- kombinations
- From: Stephan Lukits
- kombinations
- Prev by Date: kombinations
- Next by Date: [solved] Re: kombinations
- Previous by thread: kombinations
- Next by thread: [solved] Re: kombinations
- Index(es):
Relevant Pages
|
|