Re: [eclipse-clp-users] List to arrays

From: Joachim Schimpf <jschimpf_at_coninfer.com>
Date: Wed, 22 Jun 2016 12:43:49 +0100
On 22/06/16 11:19, Annick Fron wrote:
> Hi,
>
> I have a small program like this (see below). I want to generate a
> permutationof all matches, and put a sequence constraints on the
 > columns, but I am not able to transform the generated list into an
 > array,

To convert a list to an array, use
http://eclipseclp.org/doc/bips/kernel/termmanip/array_list-2.html

In your program, you know the list length in advance, so you could
also avoid the list completely, create an array in advance (dim/2)
and then fill in the elements.


 > 	... and I get into infinite loops when I ask:
 > listeDesMatches(Liste,7), X is Liste[2].

The infinite loops happen when you backtrack into listeDesMatches/2.
They are caused by your predicates with the pattern

     p(N,N).
     p(I,N) :- I1 is I+1, p(I1,N).

You can either add the missing logical condition I<N, i.e.

     p(N,N).
     p(I,N) :- I<N, I1 is I+1, p(I1,N).

or, more efficiently, use

     p(I,N) :-
         ( I<N ->
             I1 is I+1, p(I1,N)
         ;
             true
         ).


 > What should I do ?

Here is a different way to make the kind of list you want:

     listeDesMatches(Rows, N) :-
         findall(Row, (
             dim(Row,[N]),
             Row #:: 0..1,
             sum(Row[1..N]) #= 2,
             labeling(Row)
         ), Rows).


-- Joachim
Received on Wed Jun 22 2016 - 12:10:35 CEST

This archive was generated by hypermail 2.2.0 : Tue Jun 28 2016 - 18:13:13 CEST