Newsgroups: rec.arts.int-fiction
Path: news.duke.edu!newsgate.duke.edu!news-hog.berkeley.edu!ucberkeley!newsfeed.stanford.edu!paloalto-snf1.gtei.net!news.gtei.net!news.compaq.com!uunet!sac.uu.net!ash.uu.net!world!buzzard
From: buzzard@world.std.com (Sean T Barrett)
Subject: Re: [Inform Question] Test for identical objects
Message-ID: <GF93Cy.60F@world.std.com>
Date: Wed, 20 Jun 2001 22:48:34 GMT
References: <9gqvp7$8kb$1@diana.bcn.ttd.net> <3b310b6b$0$1926$b45e6eb0@senator-bedfellow.mit.edu>
Organization: The World Public Access UNIX, Brookline, MA
Lines: 57
Xref: news.duke.edu rec.arts.int-fiction:88967

Jake Wildstrom <wild_dj@mit.edu> wrote:
>OK, this actually isn't very easy. The simplest way I can see to
>do it requires 2 local variables:

Routines are your friend!

  [ FindCoinIn loc x;
     objectloop(x in loc)
        if (x ofclass GoldCoin) return x;
     return nothing;
  ];

You can use the return value of "FindCoinIn" as a coin to
be manipulated, or just use it as a boolean "true/false" to
decide if there are any coins in it:

   if (FindCoinIn(player))
      "Player has at least one coin!";
   else
      "Player has no coins.";

You could even use a "FindClassIn":

  [ FindClassIn cl loc x;
     objectloop(x in loc)
        if (x ofclass cl) return x;
     return nothing;
  ];

  ...
  if (FindClassIn(GoldCoin, player))
     ...

Want to move all the coins in an object into the player? No problem.

   ...
   while ((x = FindCoinIn(obj)) ~= nothing)
      move x to player;

Note that this could be grossly inefficient, e.g. if you had N objects,
the last N/2 of which were coins, it could be less efficient than
just "objectloop(x)" would be. But there are plenty of times when
inefficiency is not a problem.

>Maybe inform should have a command-line option for
>"lazy-designer's objectloop" where it instead expands as
>
>x=child(y) while(x){ z=sibling(x); ...  x=z; }
>
>which would break a lot less often, but is slower and burns a local
>variable.

If it doesn't *always* work, I wouldn't bother. And, unfortunately,
making it *always* work is a huge endeavor.

SeanB
see http://world.std.com/~buzzard/computers/iterate.html for a long explanation
