Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!bloom-beacon!mit-eddie!uw-beaver!ubc-cs!fs1!fs0.ee.ubc.ca!vincel From: vincel@fs0.ee.ubc.ca (vincent li) Newsgroups: comp.lang.prolog Subject: loops in prolog Keywords: loops Message-ID: <1032@fs1.ee.ubc.ca> Date: 8 Feb 90 22:41:15 GMT Sender: news@fs1.ee.ubc.ca Lines: 40 Question: how would you code the following loop in (Edingburgh) Prolog? loop: action_1; IF condition_1 THEN action_2; goto loop END IF action_3; Granted this is bad code, but the idea is to perform action_1 and action_2 within the loop. action_1 would change some data, say, and condition_1 would check the result. If condition_1 is satisfied, then action_2 is performed and stays in the loop, otherwise rest of the program is continued with action_3 WITHOUT doing action_2. The code I came up with looks something like this: loop :- action_1, (condition_1 -> action_2,loop), action_3. This works, but the recursive call eats up memory and I get out of heap errors. Is there some other way of doing this? A fail driven loop seems an obvious alternative, but action_1 is a proven-once predicate (ie. fails on backtrack). So, this doesn't seem to work: loop :- action_1,condition_1,action_2,fail. loop :- action_3. Thanks in advance for any suggestions. Vince. -------------------------------------------- vincel@ee.ubc.ca vinceli@triumfcl.bitnet vincent@graham.uucp .