From: wdbh User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.prolog Subject: Re: Prolog Help Urgent plz References: <81047b5e.0212191713.641244ae@posting.google.com> In-Reply-To: <81047b5e.0212191713.641244ae@posting.google.com> X-Enigmail-Version: 0.71.0.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 113 Message-ID: [revised] Date: Sun, 22 Dec 2002 01:31:05 GMT NNTP-Posting-Host: 209.148.113.38 X-Complaints-To: abuse@sonic.net X-Trace: typhoon.sonic.net 1040520665 209.148.113.38 (Sat, 21 Dec 2002 17:31:05 PST) NNTP-Posting-Date: Sat, 21 Dec 2002 17:31:05 PST Zarkalif wrote: > new in prolog i am trying to do this > > write a predicate between two numbers N1 and N2 > > for example > > if i type: > ?- number_between(4,9,X) > X=4 > X=5 > X=6 > X=7 > X=8 > X=9 > > thank you everyone for your help I have a sign on my wall that says DRAW A PICTURE! I might think of the integers from 4 to 9 as the list [4,5,6,7,8,9] All the members of this list are certainly between 4 and 9, so maybe a solution to the problem of generating a list of the integers from Y to Z will suggest a solution to the problem of generating the integers from Y to Z individually: list_from(Y,Y,[Y]). /* [Y] */ list_from(Y,Z,[Y|L]) :- /* [Y|[Y+1,...,Z]] */ Y < Z, Yplus1 is Y+1, list_from(Yplus1,Z,L). /* [Y+1,...,Z] */ ?- list_from(4,9,L). L = [4,5,6,7,8,9] ? ; No more answers. Hmm, maybe I can extract the members of the list as it is being generated. member_list_from(Y,Y,Z,[Y|L]). member_list_from(X,Y,Z,[Y|L]) :- Y < Z, Yplus1 is Y+1, member_list_from(X,Yplus1,Z,L). ?- member_list_from(X,4,9,L). L = [4|_] X = 4 ? ; L = [4,5|_] X = 5 ? ; L = [4,5,6|_] X = 6 ? ; L = [4,5,6,7|_] X = 7 ? ; L = [4,5,6,7,8|_] X = 8 ? ; L = [4,5,6,7,8,9|_] X = 9 ? ; No more answers. Ok, that worked, but I really don't need to actually see the list, so I'll just drop it. member_imaginary_list(Y,Y,_). member_imaginary_list(X,Y,Z) :- Y < Z, Yplus1 is Y+1, member_imaginary_list(X,Yplus1,Z). ?- member_imaginary_list(X,4,9). X = 4 ? ; X = 5 ? ; X = 6 ? ; X = 7 ? ; X = 8 ? ; X = 9 ? ; No more answers. And, whereas the members of my imaginary list of the integers from Y to Z are the integers from Y to Z, that should do it. Bill - http://www.sonic.net/~sequitur