Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!zombie.ncsc.mil!news.duke.edu!godot.cc.duq.edu!newsfeed.pitt.edu!gatech!newsxfer.itd.umich.edu!nntp.cs.ubc.ca!unixg.ubc.ca!news.bc.net!newsserver.sfu.ca!fornax!jamie
From: jamie@cs.sfu.ca (Jamie Andrews)
Subject: Re: Local varible possible for Prolog?
Message-ID: <1994Nov30.183757.29520@cs.sfu.ca>
Organization: Faculty of Applied Science, Simon Fraser University
References: <3bgkbc$5va@nic-nac.CSU.net>
Date: Wed, 30 Nov 1994 18:37:57 GMT
Lines: 49

In article <3bgkbc$5va@nic-nac.CSU.net>,
Yuan-chi (Bill) Chiu <hbcsc112@huey.csun.edu> wrote:
>   Because prolog's varible can only have one value, I found it very
>difficult to do some of the simple things in other languages such as
>having a temporary varible that grows inside a loop....

     Bill, forgive me if I'm wrong, but I get the impression
that you may be making the common beginning-Prolog mistake of
trying to do things in Prolog in the way you are used to doing
it in an imperative language, or conversely in trying to take
advantage of Prolog's "database" features too much.

     One common aspect of this is to try to make something be a
predicate (stored in Prolog's database of clauses) instead of as
a data structure passed as a parameter.  Instead of representing
the apples you have as clauses, e.g.

apple(mac, red).
apple(granny_smith, green).
apple(golden_delicious, yellow).

...it may be more appropriate to represent them as a list
[ apple(mac, red),
  apple(granny_smith, green),
  apple(golden_delicious, yellow) ]
...and then pass the list as a parameter.  Then you can do
manipulation of the data structure, which seems to be what you
are trying to do with loops and clauses.

>   This implementation of findall using asserta of found(_) is dangerous.
>Because it influences the global state -- maybe some other functions
>may also use the found(_) in a different way....

     If you really do need findall (it is necessary for some
problems, but not as many as beginning Prolog programmers often
think), then it's much better to program it using
assert/retract isolated from the rest of the program, instead of
using assert/retract in the main program.  You're right in that
"found" and "mark" are a bit too likely to be used by other
programs -- maybe you should use some bizarre names like
"fInDaLl_found" and "fInDaLl_mark" or something.

     But as I say, it may be that you don't need findall at all,
but just need to record data in a data structure instead of in
the clause database.

--Jamie.
  jamie@cs.sfu.ca
"Make sure Reality is not twisted after insertion"
