Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!mcvax!botter!klipper!biep
From: biep@cs.vu.nl (J. A. "Biep" Durieux)
Newsgroups: comp.lang.prolog
Subject: Re: Prolog riddle - (nf)
Message-ID: <814@klipper.cs.vu.nl>
Date: Thu, 16-Jul-87 04:31:06 EDT
Article-I.D.: klipper.814
Posted: Thu Jul 16 04:31:06 1987
Date-Received: Sat, 18-Jul-87 05:42:34 EDT
References: <29400001@uklirb.UUCP> <1343@sics.se>
Reply-To: biep@cs.vu.nl (J. A. "Biep" Durieux)
Organization: VU Informatica, Amsterdam
Lines: 29

In article <1343@sics.se> lhe@sics.se (Lars-Henrik Eriksson) writes:
>In article <29400001@uklirb.UUCP> noekel@uklirb.UUCP writes:
>>does anyone out there in netland know whether it is possible to write a Prolog
>>program that 
>>
>>(1) *generates* natural numbers in ascending order,
>>(2) does not make use of side effects (assert, retract etc.),
>>(3) generates the first n numbers in time O(n).
>>
>
>If you want the predicate to generate successive integers each time it is
>called, then it is impossible to do without side effects.

I believe what he wants is something like:

numbers(X) :- count(0, X).          ; or whatever number you want to start with

count(X, X).
count(X, Y) :- Z = X+1, count(Z, Y).

In this way, *on backtracking*, all natural numbers are generated.
(Thanks to Hans Weigand, who once helped me out of a similar problem).

I think this program meets the three specified criteria, except perhaps
that due to interpreter inefficiencies time might exceed O(n).
-- 
						Biep.  (biep@cs.vu.nl via mcvax)
	Net weight is determined by considering contents only.
				(From: How to become an important usenet person)