Re: [eclipse-clp-users] "twice" similar to "once"

From: Marco Gavanelli <marco.gavanelli_at_unife.it>
Date: Tue, 22 Mar 2016 08:36:47 +0100
On 22/03/16 04:18, Edgaonkar, Shrirang wrote:
> Similar to "once / 1" I would like to write "twice / 1" goal so that
> the first solution backtracks and finds the second solution. I wish
> to get both the solutions.

In order to get two solutions (without recomputing the first), you need 
to store some information in a way that survives backtracks.

The first idea that comes to my mind is to use assert and retract, that 
are standard and work in all Prolog compilers:

:- dynamic previously_solved/0.

twice(Goal):-
	call(Goal),
	(previously_solved
	->	!, retract(previously_solved)
	;	assert(previously_solved)).

In ECLiPSe you can have better, reentrant code by using non-logical 
storage predicates:

http://eclipseclp.org/doc/userman/umsroot050.html

Best,
Marco

-- 
http://docente.unife.it/marco.gavanelli
Received on Tue Mar 22 2016 - 08:00:46 CET

This archive was generated by hypermail 2.2.0 : Tue Mar 22 2016 - 12:13:17 CET