Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!comp.vuw.ac.nz!virtue!canterbury.ac.nz!fore057 From: fore057@canterbury.ac.nz Newsgroups: comp.lang.prolog Subject: Re: Prolog Prog. Technique Question Message-ID: <1990Aug13.173019.8912@canterbury.ac.nz> Date: 13 Aug 90 06:06:50 GMT References: Organization: University of Canterbury Lines: 51 In article , tg1e+@andrew.cmu.edu (Timothy R. Gottschalk) writes: > I took a course that introduced Prolog syntax, but never touched prog. > techniques. Thus, I am feeling my way through. I am writing code similiar > to the following structure (in Turbo Prolog V2.0): > > loop(In):- > check_mouse, > do_eval(In, Out), > loop(Out). > loop(_):- > do_exit. > > The compiler recognizes that it is tail-recursive, but still allocates memory > off the heap during each iteration. As the loop should only be run when > the mouse button is pressed, the check_mouse succeeds when the button is > pressed, but fails otherwise. Commenting out the check_mouse or do_eval > call does not necessarily stop the heap allocation, although commenting > out both certainly does (of course). The easiest solution would be to > make a predicate fail somewhere, but, then, how would I be able to get a > return value? (Using reference vars or internal dbases seems to make the > program run unacceptably slow). > > Tim G. If you want both input to and output from a predicate, it must have a arity greater than one: loop(In,Out):- check_mouse, process(In,Out). It's not clear from your example why you need a recursive structure. However, Prolog will eat lots of memory if a non-deterministic predicate is called within the clause. To avoid this, ensure that called predicates are non-deterministic, or use a cut after the non-deterministic call: loop(In,In):- condition(satisfied). loop(In,Out):- process(In,In2),!, %"process" is non-deterministic .... loop(In2,Out). Note that variable "Out" remains unbound until the desired condition is satisfied. 'Hope this helps, Euan Mason University of Canterbury, NZ.