From pcortelli@inwind.it  Wed Dec 20 20:32:20 2000
Received: from relay4.inwind.it (relay4.inwind.it [212.141.53.75])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id UAA04106
	for <prolog@swi.psy.uva.nl>; Wed, 20 Dec 2000 20:32:19 +0100 (MET)
Received: from cortelli (62.98.127.144) by relay4.inwind.it (5.1.056)
        id 3A40C07E0000F580 for prolog@swi.psy.uva.nl; Wed, 20 Dec 2000 20:32:17 +0100
Message-Id: <3.0.6.32.20001220111740.007b31f0@popmail.inwind.it>
X-Sender: pcortelli@popmail.inwind.it (Unverified)
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Wed, 20 Dec 2000 11:17:40 +0100
To: prolog@swi.psy.uva.nl
From: Cortelli Paolo <pcortelli@inwind.it>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Subject: [SWIPL] Backtracking

Hi,
   I', trying to create a list with numbers whose sum equals an imposed
limit. All is ok, but I wanted to stop going on when there are no possible
solutions. Of course it a job for the cut, but I can't find the right place
to put it. Can someone help me? 

   Here is the source.

go(Limit,List) :- 
  put_in_list(Limit,[],Num_list),!,
  make_numbers(Limit,0,Num_list,List).


% create a list with the numbers
% ------------------------------
  put_in_list(0,L,L) .
  put_in_list(Limit,L,Num_list) :- 
     Limit1 is Limit - 1,
     Lx = [Limit|L],
     put_in_list(Limit1,Lx,Num_list).

% make a list with all numbers to sum up to Limit
% -----------------------------------------------
  make_numbers(Limit,Limit,_,[]).
  make_numbers(Limit,Sum,Num_list,[T|C]):- 
   select(T,Num_list,Rest),
   plus(T,Sum,S1), 
   S1 =< Limit,	
   make_numbers(Limit,S1,Rest,C).

If S1 =< Limit, then all the list is scanned before before backtracking to
the previous recorsive level.
	

Thanks for your help.                 Paolo Cortelli

