Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #45089
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: rosetta code compare list of strings |
| Date | 2016-01-26 15:50 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2016Jan26.165040@mips.complang.tuwien.ac.at> (permalink) |
| References | <7bb73814-3c0b-4d35-bdd3-0542393a56fe@googlegroups.com> |
foxaudioresearch@gmail.com writes:
>http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth
This example is interesting, because Forth-2012 actually has a kind of
list of strings, and a word for visiting the elements of such a list:
wordlists and TRAVERSE-WORDLIST. So let's take a look at how this
example might look with these tools:
: compare-mask ( c-addr1 u1 c-addr2 u2 -- x )
\ x is 1 if s1<s2, 2 if s1=s2, 4 if s1>s2
compare 1+ 1 swap lshift ;
: comp-names-helper ( nt1 x1 nt2 -- nt2 x2 x2 )
swap >r over if
tuck name>string rot name>string 2swap compare-mask r> and dup
else
nip r> dup
then ;
: comp-names ( wid -- x )
>r 0 7 ['] comp-names-helper r> traverse-wordlist nip ;
: names= ( wid -- f )
comp-names 2 and 0<> ;
: names-ascending ( wid -- f )
comp-names 1 and 0<> ;
\ test code
wordlist constant test1
get-current test1 set-current
create abc
create abc
create abc
create abc
set-current
wordlist constant test2
get-current test2 set-current
create abe
create abd
create abc
create abb
create ab
create aaz
set-current
test1 names= .
test2 names= .
test1 names-ascending .
test2 names-ascending .
Several things to note:
TRAVERSE-WORDLIST is not guaranteed to return the words in the reverse
order of definition (only for same-named words, where it does not
matter for this example), but at least in Gforth it does.
We need to special-case the first iteration here, because we need to
compare two consecutive elements here, and TRAVERSE-WORDLIST gives us
one element per iteration (so on the first iteration we are missing
the previous element). The result may be more complicated than an
explicit walk over a linked list that is designed for this kind of
work.
NAMES= and NAMES-ASCENDING are almost completely equal, except for the
actual comparison. A straightforward way to deal with this is to pass
the comparison into COMP-NAMES and COMP-NAMES-HELPER as an xt, and
EXECUTE it there, but this would complicate the stack handling even
more. Another way, discussed several times (as closures) would be to
define the xt as local in COMP-NAMES, use a closure (a quotation with
access to outer locals) in COMP-NAMES instead of defining
COMP-NAMES-HELPER, and the do "xt EXECUTE" in the closure.
Here I took a different route by computing both results at the same
time, passing them both out, and then selecting between the results in
the end. The disadvantage of this approach is that the loop may
terminate later than if we test for one result only.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2015: http://www.rigwit.co.uk/EuroForth2015/
Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread
rosetta code compare list of strings foxaudioresearch@gmail.com - 2016-01-20 21:53 -0800
Re: rosetta code compare list of strings mhx@iae.nl - 2016-01-20 22:25 -0800
Re: rosetta code compare list of strings Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-01-21 11:02 +0000
Re: rosetta code compare list of strings franck.bensusan@gmail.com - 2016-01-21 23:38 -0800
Re: rosetta code compare list of strings Doug Hoffman <glidedog@gmail.com> - 2016-01-22 05:48 -0500
Re: rosetta code compare list of strings "WJ" <w_a_x_man@yahoo.com> - 2016-01-23 03:39 +0000
Re: rosetta code compare list of strings Julian Fondren <julian.fondren@gmail.com> - 2016-01-22 21:26 -0800
Re: rosetta code compare list of strings Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-01-23 10:02 +0000
Re: rosetta code compare list of strings mhx@iae.nl - 2016-01-23 00:41 -0800
Re: rosetta code compare list of strings Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-01-23 10:15 +0000
Re: rosetta code compare list of strings Julian Fondren <julian.fondren@gmail.com> - 2016-01-20 23:15 -0800
Re: rosetta code compare list of strings "WJ" <w_a_x_man@yahoo.com> - 2016-01-21 08:34 +0000
Re: rosetta code compare list of strings Doug Hoffman <glidedog@gmail.com> - 2016-01-21 09:00 -0500
Re: rosetta code compare list of strings "HAA" <someone@microsoft.com> - 2016-01-23 00:35 +1100
Re: rosetta code compare list of strings humptydumpty <ouatubi@gmail.com> - 2016-01-22 15:03 -0800
Re: rosetta code compare list of strings anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-01-26 15:50 +0000
csiph-web