Re: n-queens version in c

From: <wh_at_icparc.ic.ac.uk>
Date: Tue 17 Aug 2004 04:50:30 PM GMT
Message-ID: <20040817165030.GA19980@tempest.icparc.ic.ac.uk>
Hi Christian,

May I ask why you want to do this?  I haven't looked at why it doesn't work:
it's extremely difficult to read - I'd hate to try to maintain code like
this!

I suggest that if you want to use ECLiPSe's control structures (do loops,
if-then-else, etc.), write your program in ECLiPSe.  If you want to write
your program in C++, then use C++'s control structures and just post the
constraints to ECLiPSe.

Also, when requesting help, you can help others to help you if you provide
more of an explanation than just "It didn't work".  In what way didn't it
work?

Cheers,
Warwick

On Tue, Aug 17, 2004 at 10:34:19AM -0300, masdeval wrote:
> 
>     Friends, 
> 
>     i'am trying to codify the n-queens problem like it appears in the eclipse home page using the eclipse c++ interface (see below). Someone could see what's wrong? It didn't work but to me is everything correct.
> 
>     Thanks, Christian
> 
>     /* n-queens */
>   /*queens_lists(N, Board) :-
> 
>  length(Board, N),
>  Board :: 1..N,
>  ( fromto(Board, [Q1|Cols], Cols, []) do
>      ( foreach(Q2, Cols), param(Q1), count(Dist,1,_) do
>   Q2 #\= Q1,
>   Q2 - Q1 #\= Dist,
>   Q1 - Q2 #\= Dist
>      )
>  ), labeling(Board).  */
> 
> /* C++ version */ 
> 
>   EC_ref Board, N;
>   EC_word Q1,Cols,Q2,Dist, Underline;
>   Q1 = newvar();
>   Cols = newvar();
>   Q2 = newvar();
>   Dist = newvar();
>   Underline = newvar();
> 
>   EC_word wlist = nil();
>   wlist = list(Cols,wlist);
>   wlist = list(Q1,wlist);
> 
>   post_goal("lib(ic)");
>   //length(Board, N), Board :: 1..N,
>   EC_word term1 = term(EC_functor(",",2), term(EC_functor("length",2),Board,N), term(EC_functor("::",2),Board, term(EC_functor("..",2),1,N)));
> 
>   //fromto(Board, [Q1|Cols], Cols, [])
>   EC_word term2 = term(EC_functor("fromto",4), Board, wlist, Cols, nil());
> 
>   //foreach(Q2, Cols), param(Q1), count(Dist,1,_)
>   EC_word term3 = term(EC_functor(",",2), term(EC_functor("foreach",2),Q2,Cols), term(EC_functor("param",1),Q1) );
>   EC_word term4 = term(EC_functor(",",2), term3, term(EC_functor("count",3),Dist,1,Underline));
> 
>   //Q2 #\= Q1, Q2 - Q1 #\= Dist,Q1 - Q2 #\= Dist
>   EC_word term5 = term(EC_functor(",",2),term(EC_functor("#\\=",2),Q2,Q1) , term(EC_functor("#\\=",2), term(EC_functor("-",2),Q2,Q1),Dist) );
>   EC_word term6 = term(EC_functor(",",2),term5, term(EC_functor("#\\=",2), term(EC_functor("-",2),Q1,Q2), Dist ) );
> 
>   // second do
>   EC_word term7 = term(EC_functor("do",2),term4,term6);
>   //first do
>   EC_word term8 = term(EC_functor("do",2), term2, term7 );
>   
>  //labeling
>   EC_word term9 = term(EC_functor("labeling",1),Board);
>   
>   EC_word term10 = term(EC_functor(",",2),term1,term8);
>   EC_word term11 = term(EC_functor(",",2),term10, term9 );
> 
>   EC_word body = term( EC_functor(",",2), term11, term(EC_functor("writeln",1),Board ));
>    
>   post_goal( term( EC_functor("assert",1), term(EC_functor(":-",2),  term(EC_functor("queens_lists",2),N, Board), body )));
> 
>   EC_ref aux;
>   aux = newvar();
>   post_goal(term(EC_functor("queens_lists",2),4,aux));
> 
>   if(EC_resume() == EC_succeed)
>     printf(" Success ");
>  
>    
Received on Tue Aug 17 17:55:59 2004

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:29 PM GMT GMT