Message from discussion
Words consuming arguments, was [Re: Is there a better way?]
Received: by 10.68.135.2 with SMTP id po2mr34755pbb.0.1352178447780;
Mon, 05 Nov 2012 21:07:27 -0800 (PST)
MIME-Version: 1.0
Received: by 10.68.242.74 with SMTP id wo10mr5904pbc.9.1352178447764; Mon, 05
Nov 2012 21:07:27 -0800 (PST)
Path: 6ni63996pbd.1!nntp.google.com!kr7no33496290pbb.0!postnews.google.com!6g2000pbh.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Mon, 5 Nov 2012 21:07:27 -0800 (PST)
Complaints-To: groups-abuse@google.com
Injection-Info: 6g2000pbh.googlegroups.com; posting-host=71.223.121.220; posting-account=hP9USgoAAADyPwMdR5qi1rinwP694_5o
NNTP-Posting-Host: 71.223.121.220
References: <70a117c1-ba9f-474b-97f3-6440be3d4573@k20g2000vbj.googlegroups.com>
<k6rfsn$ue4$1@speranza.aioe.org> <k6rgi9$nd$1@speranza.aioe.org>
<k6u352$sbp$3@dont-email.me> <5a70f750-2b4b-4c71-962c-0906bdb5f969@vy11g2000pbb.googlegroups.com>
<k70uhq$j2u$1@dont-email.me> <782dnUcywZ3snQnNnZ2dnUVZ8uOdnZ2d@supernews.com>
<k713o5$lbp$2@dont-email.me> <6c7a026e-ea57-4bd3-8ca6-0ec7a3d66f98@q4g2000vbg.googlegroups.com>
<0fCdnZJDo-5tswvNnZ2dnUVZ8nWdnZ2d@supernews.com> <bfe98488-6f97-497f-9a5f-1959d63759eb@b19g2000vbt.googlegroups.com>
<Tq2dncwBFL7RKAvNnZ2dnUVZ_qGdnZ2d@supernews.com> <62cb4769-1a1e-4095-9474-edb37cb9053d@pb2g2000pbc.googlegroups.com>
<ccc298bd-9de6-4def-a4c6-4f31c17af369@ez26g2000vbb.googlegroups.com>
<k7926a$aob$1@speranza.aioe.org> <o6WdnVkWKcFjvgXNnZ2dnUVZ_sudnZ2d@supernews.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4
(KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4,gzip(gfe)
Message-ID: <aafcb3a4-dab1-4390-8e10-b894284686c2@6g2000pbh.googlegroups.com>
Subject: Re: Words consuming arguments, was [Re: Is there a better way?]
From: Hugh Aguilar <hughaguila...@yahoo.com>
Injection-Date: Tue, 06 Nov 2012 05:07:27 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Nov 5, 1:39=A0pm, "Elizabeth D. Rather" <erat...@forth.com> wrote:
> On 11/5/12 9:01 AM, Rod Pemberton wrote:
> > I take it this is to make sure extra arguments aren't left on the stack=
as a
> > primitive form of "error checking". =A0However, I really don't see the =
point
> > of this. =A0In certain situations, the style that breaks the "rule" may=
be
> > create more effective coding.
>
> > As I see it (AISI), the primary effect of this "rule" is to shift the
> > location where parameters are duplicated or copied, and/or dropped. =A0=
If a
> > word preserves arguments, then the copying is internal. =A0If it doesn'=
t, then
> > external. =A0While the example below with the DROP has the disadvantage
> > of using a DROP, it has the advantage that it can modify 'addr'. =A0Thi=
s
> > seems to be very useful for parsing words and linked-lists. =A0Isn't it=
?
>
> > : OPR ( addr -- addr ) DUP ... ;
> > ' addr' OPR OPR OPR OPR DROP
>
> > : OPR ( addr -- ) ... ;
> > 'addr' DUP OPR DUP OPR DUP OPR OPR
>
> Sure, there are situations when you're coding part of a process that
> needs to pass things right through the process. The classic example
> would be the output number formatting words:
>
> <# starts the process, taking a double and initializing the string
> output area.
>
> Then words like # and #s do things to the double, affecting the string
> under construction and passing on the modified double, and the process
> is completed by #> .
>
> But note that # etc. are *useless* outside of this particular context.
> That is the tradeoff. Most of the time, it's better to code the word
> such that it isn't context-dependent, even though the first time you
> invent it it's within a specific context.
>
> Cheers,
> Elizabeth
This is what I meant earlier by the word having a "ritualistic" use.
My example was INIT-LIST. This is used in a very specific context and
always in the same way.
Whenever you create a node, the first thing that you do with the
address is initialize it for whatever type it is going to be, and the
datum (the node address) is being carried along. This is similar to
how # is used only inside of <# and #>, and the datum (the double-
precision integer) is being carried along.
Putting the DUP (or 2DUP or whatever) inside the word helps with
efficiency because the ritualistic word is typically so commonly used
that you will likely write in in assembly-language.
If the word isn't used in a ritualistic manner though, then you don't
know if the datum is going to be needed afterward or not. You may
either have a DUP before the word (if it consumes its argument) or a
DROP after the word (if it doesn't). It is best to be consistent and
have the words consume their arguments. This way there is no confusion
about whether you need a DUP or a DROP. Also, it tends to be more
efficient because the code inside of the word consumes the data and
you don't have an explicit DROP.
As an example, here are two ways to write the + word:
: + ( a b -- a+b )
which is used like this:
5 7 + .
12 ok
or
: + ( a b -- a b a+b )
which is used like this:
5 7 + . 2drop
12 ok
Pretty obvious which makes for the most readable code, don't you
think?