Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #44982 > unrolled thread

rosetta code compare list of strings

Started byfoxaudioresearch@gmail.com
First post2016-01-20 21:53 -0800
Last post2016-01-26 15:50 +0000
Articles 16 — 10 participants

Back to article view | Back to comp.lang.forth


Contents

  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

#44982 — rosetta code compare list of strings

Fromfoxaudioresearch@gmail.com
Date2016-01-20 21:53 -0800
Subjectrosetta code compare list of strings
Message-ID<7bb73814-3c0b-4d35-bdd3-0542393a56fe@googlegroups.com>
I have been bad again. 

I know some here may not like it but I am attempting to help explain Forth in
these demonstrations, versus creating "gee whiz" code. (which I can't do)

So the code is dumbed down. This works for well for me given my intelligence.:-)

http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth

BF

P.S.

The poor guy who wrote the Fortran code demonstration wrote multiple paragraphs
of sob story about missing features in the language. 
It makes Forth look pretty good.

[toc] | [next] | [standalone]


#44983

Frommhx@iae.nl
Date2016-01-20 22:25 -0800
Message-ID<86921d4e-17a0-43e8-8dc0-028cf32d5392@googlegroups.com>
In reply to#44982
On Thursday, January 21, 2016 at 6:53:35 AM UTC+1, foxaudio...@gmail.com wrote:
> I have been bad again. 
> 
> I know some here may not like it but I am attempting to help explain Forth in
> these demonstrations, versus creating "gee whiz" code. (which I can't do)
> 
> So the code is dumbed down. This works for well for me given my intelligence.:-)
> 
> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth
> 
> BF
> 
> P.S.
> 
> The poor guy who wrote the Fortran code demonstration wrote multiple paragraphs
> of sob story about missing features in the language. 
> It makes Forth look pretty good.

I think you did much more than required. For instance, 
the C implementation assumes that the strings are 
presented as a known-size array of pointers to 
strings (a list of pointers as you will) and gets 
very easy but difficult to read code that way. In 
Forth you can make the same assumptions and have 
one-line poetry instead.

-marcel

[toc] | [prev] | [next] | [standalone]


#44988

FromAlbert van der Horst <albert@spenarnc.xs4all.nl>
Date2016-01-21 11:02 +0000
Message-ID<56a0bac6$0$23801$e4fe514c@news.xs4all.nl>
In reply to#44983
mhx@iae.nl writes:

>On Thursday, January 21, 2016 at 6:53:35 AM UTC+1, foxaudio...@gmail.com wrote:
>> I have been bad again.
>>
>> I know some here may not like it but I am attempting to help explain Forth in
>> these demonstrations, versus creating "gee whiz" code. (which I can't do)
>>
>> So the code is dumbed down. This works for well for me given my intelligence.:-)
>>
>> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth
>>
>> BF
>>
>> P.S.
>>
>> The poor guy who wrote the Fortran code demonstration wrote multiple paragraphs
>> of sob story about missing features in the language.
>> It makes Forth look pretty good.

>I think you did much more than required. For instance,
>the C implementation assumes that the strings are
>presented as a known-size array of pointers to
>strings (a list of pointers as you will) and gets
>very easy but difficult to read code that way. In
>Forth you can make the same assumptions and have
>one-line poetry instead.

Very interesting problem indeed ;-) , applying COMPARE in a loop.

A much more practical (as in actually occuring) problem would be
"given a text file, build a data structure in memory that allows
lines to be accessed by their line number. Demonstrate it by
having a function to type line N"

>-marcel
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#45017

Fromfranck.bensusan@gmail.com
Date2016-01-21 23:38 -0800
Message-ID<3a266c8c-8c12-4393-8ca8-8a22d18a076d@googlegroups.com>
In reply to#44988
Le jeudi 21 janvier 2016 12:02:52 UTC+1, Albert van der Horst a écrit :
> A much more practical (as in actually occuring) problem would be
> "given a text file, build a data structure in memory that allows
> lines to be accessed by their line number. Demonstrate it by
> having a function to type line N"
> 
> Albert van der Horst, UTRECHT,THE NETHERLANDS
> Economic growth -- being exponential -- ultimately falters.
> albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Hi,

In Oforth, text file objects can be handled as a collection of lines and 
respond to higher-order methods like apply, map, ... 

For instance, to create a constant which value is the list of all lines of a 
file (a collection of strings) : 

"INSTALL.TXT" File new map( #yourself ) const: FLINES

: typeAt     FLINES at . ;      // n -- 

Franck

[toc] | [prev] | [next] | [standalone]


#45021

FromDoug Hoffman <glidedog@gmail.com>
Date2016-01-22 05:48 -0500
Message-ID<56a208e8$0$304$14726298@news.sunsite.dk>
In reply to#44988
On 1/21/16 6:02 AM, Albert van der Horst wrote:

> A much more practical (as in actually occuring) problem would be
> "given a text file, build a data structure in memory that allows
> lines to be accessed by their line number. Demonstrate it by
> having a function to type line N"

It's a pretty trivial problem if one first has list and string library
code. For example:

Works with any ANS Forth

Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
include FMS-SI.f
include FMS-SILib.f

1000 constant maxchars-per-line

file f
object-list list

: list-read ( faddr len -- )
     f new: f open: throw
     begin
       maxchars-per-line f read-line:
     while
       @: heap> string+ dup >r !:
       r> list add:
     repeat f close: throw ;

\ text file from the Brodie SF example
s" my-data-file.txt" list-read

5 list at: p: Savitch,Jessica,newscaster,555-9653
20 list at: p:  Jabbar,Kareem Abdul,basketball player,555-4439

List-read could easily be made a method of class file. Then the library
code would be approaching the simplicity of Franck Bensusan's nice
Oforth example.

-Doug

[toc] | [prev] | [next] | [standalone]


#45036

From"WJ" <w_a_x_man@yahoo.com>
Date2016-01-23 03:39 +0000
Message-ID<n7uslo$qjr$1@dont-email.me>
In reply to#44988
Albert van der Horst wrote:

> A much more practical (as in actually occuring) problem would be
> "given a text file, build a data structure in memory that allows
> lines to be accessed by their line number. Demonstrate it by
> having a function to type line N"

That's so simple that it's not a problem unless one is volunteering
to be a victim of the perpetrators of the toy language ANS Forth.

Ruby:

IO.readlines("test.csv")[2].display
 ===>
Bill,22,M

-- 
Amazon bans book.  After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo

[toc] | [prev] | [next] | [standalone]


#45037

FromJulian Fondren <julian.fondren@gmail.com>
Date2016-01-22 21:26 -0800
Message-ID<5b48c0dd-5028-43ba-96a1-0fd1df942f65@googlegroups.com>
In reply to#45036
On Friday, January 22, 2016 at 9:42:29 PM UTC-6, WJ wrote:
> Albert van der Horst wrote:
> 
> > A much more practical (as in actually occuring) problem would be
> > "given a text file, build a data structure in memory that allows
> > lines to be accessed by their line number. Demonstrate it by
> > having a function to type line N"
> 
> That's so simple that it's not a problem [...]
> 
> Ruby:
> 
> IO.readlines("test.csv")[2].display
>  ===>
> Bill,22,M

  $ wc -l lines.txt
  41120627 lines.txt

  $ du -sh lines.txt
  2.0G    lines.txt

  $ /usr/bin/time -f "real %e\nuser %U\nsys %S\nmem %M\nFS reads: %I\n" ruby -e 'IO.readlines("lines.txt")[2].display'
  pvpjhcjzcvyojcnhmbrspsadpprhkkbcigehwkwypyuncotfit
  real 41.69
  user 39.58
  sys 2.08
  mem 4856228 (4.6G)
  FS reads: 4848


  $ time sf genindex.fs

  real    0m7.188s
  user    0m6.853s
  sys     0m0.226s

  $ time sf getline.fs 2
  pvpjhcjzcvyojcnhmbrspsadpprhkkbcigehwkwypyuncotfit

  real    0m0.006s
  user    0m0.001s
  sys     0m0.005s


-- Julian

[toc] | [prev] | [next] | [standalone]


#45041

FromAlbert van der Horst <albert@spenarnc.xs4all.nl>
Date2016-01-23 10:02 +0000
Message-ID<56a34faf$0$23737$e4fe514c@news.xs4all.nl>
In reply to#45036
"WJ" <w_a_x_man@yahoo.com> writes:

>Albert van der Horst wrote:

>> A much more practical (as in actually occuring) problem would be
>> "given a text file, build a data structure in memory that allows
>> lines to be accessed by their line number. Demonstrate it by
>> having a function to type line N"

>That's so simple that it's not a problem unless one is volunteering
>to be a victim of the perpetrators of the toy language ANS Forth.

>Ruby:

>IO.readlines("test.csv")[2].display
> ===>
>Bill,22,M

Not correct. The data structure is not persistent. You must describe
how to create and use the data structure. Then print a line to prove it.

Moreover the above is a Toy example. A more practical program would
be a turnkey and have the file name and the line number from the
command line or a prompt e.g.

A> selectline test.csv 2
A> Bill,22,M

A ciforth program (wina -c selectline.frt ) would end in

: doit 1 ARG[] split-in-lines 2 ARG[] EVALUATE line[] TYPE ;

while having `` split-in-lines line[] '' as useful  factors
for Forths that can't do turnkey or understand commandline
arguments.


>--
>Amazon bans book.  After nearly a month on the site, all traces of the book and
>its 80 reviews have been removed.
>http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
>https://www.youtube.com/watch?v=EEl_1HWFRfo

I looked into this. Amazon apparently doesn't want to be a playground
for crackpots. Understandable for a commercial organisation.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#45040

Frommhx@iae.nl
Date2016-01-23 00:41 -0800
Message-ID<841ed3dd-921a-4f13-adc3-e71a8c015b2b@googlegroups.com>
In reply to#44988
On Thursday, January 21, 2016 at 12:02:52 PM UTC+1, Albert van der Horst wrote:
> 
[..]
> Very interesting problem indeed ;-) , applying COMPARE in a loop.
> 
> A much more practical (as in actually occuring) problem would be
> "given a text file, build a data structure in memory that allows
> lines to be accessed by their line number. Demonstrate it by
> having a function to type line N"

What kind of spec. is that?

"Assume that the lines are less than 64 plain ASCII characters 
long, and there are less than 16 free format lines in the text 
file."

Problem solved in this particular case. The rest is just an exercise
for the reader.

-marcel

[toc] | [prev] | [next] | [standalone]


#45042

FromAlbert van der Horst <albert@spenarnc.xs4all.nl>
Date2016-01-23 10:15 +0000
Message-ID<56a352ad$0$23737$e4fe514c@news.xs4all.nl>
In reply to#45040
mhx@iae.nl writes:

>On Thursday, January 21, 2016 at 12:02:52 PM UTC+1, Albert van der Horst wrote:
>>
>[..]
>> Very interesting problem indeed ;-) , applying COMPARE in a loop.
>>
>> A much more practical (as in actually occuring) problem would be
>> "given a text file, build a data structure in memory that allows
>> lines to be accessed by their line number. Demonstrate it by
>> having a function to type line N"

>What kind of spec. is that?

>"Assume that the lines are less than 64 plain ASCII characters
>long, and there are less than 16 free format lines in the text
>file."

>Problem solved in this particular case. The rest is just an exercise
>for the reader.

I've looked into this, and a very similar challenge is present
in rosseta. Your solution however would not make Forth look good.
The challenge demands error message for situations like a file
that is too large. Rossetta has so many examples that it is worthwhile
to search it for idiomatic ways to solve a problem in a particular
language. I see that Ada Algol68 C LISP all have an abundant
presence that helps in tackling real problems. rosetta is
much more valuable than the language shootout we once had and
the presence of Forth is a way to promote Forth. The gForth examples
I see do a good job.

>-marcel
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#44984

FromJulian Fondren <julian.fondren@gmail.com>
Date2016-01-20 23:15 -0800
Message-ID<b88c826f-4404-47a3-9e0e-7d576b588f2b@googlegroups.com>
In reply to#44982
On Wednesday, January 20, 2016 at 11:53:35 PM UTC-6, foxaudio...@gmail.com wrote:
> I have been bad again. 

As a rule, it's pretty OK to post Forth to comp.lang.forth


> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth

I like the COUNT + packed-counted-strings structure.  I used it in
an old Chinese-language study program, and in some other cases.  But
it's a lot less amusing when you want to modify anything -- the
overall structure, or even any of the elements.

One opportunity here though might be to show how you can change the
structure without changing the syntax, or many of the words.

For example, your code from COMPARE$ down, with the list definitions
and the tests, all continues to work with the following words:

  : ," ( -- c-addr u )
    [char] " parse
    dup allocate throw
    swap 2dup 2>r move 2r> ;
  0 constant [[
  : ]] ( 0 's1' 's2' 's3' ... -- )
    here cell allot begin over while -rot , , repeat
    here 2 cells - swap !  drop ;
  : nth ( n list -- addr )  @ swap 2* cells - ;
  : items ( list - n )  @+ swap - cell / 2/ ;
  : count ( addr - c-addr u )  2@ ;
 
If ]] returned an address then ," could store strings in the
dictionary and drop ( c-addr u ) pairs for ]]

If NTH returned a string instead of an address then it wouldn't be
necessary to define COUNT unusually.


-- Julian

[toc] | [prev] | [next] | [standalone]


#44985

From"WJ" <w_a_x_man@yahoo.com>
Date2016-01-21 08:34 +0000
Message-ID<n7q569$2uh$1@dont-email.me>
In reply to#44982
foxaudioresearch@gmail.com wrote:

> I have been bad again. 
> 
> I know some here may not like it but I am attempting to help explain Forth in
> these demonstrations, versus creating "gee whiz" code. (which I can't do)
> 
> So the code is dumbed down. This works for well for me given my intelligence.:-)
> 
> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth
> 
> BF
> 
> P.S.
> 
> The poor guy who wrote the Fortran code demonstration wrote multiple paragraphs
> of sob story about missing features in the language. 
> It makes Forth look pretty good.

Ruby:

strings = %w(aa bb cc dd ee ff gg)
strings.each_cons(2).all?{|a,b| a<b}
    ==>true
strings.each_cons(2).all?{|a,b| a==b}
    ==>false

strings = %w(foo foo foo foo)
strings.each_cons(2).all?{|a,b| a<b}
    ==>false
strings.each_cons(2).all?{|a,b| a==b}
    ==>true

strings = ["foo"]
strings.each_cons(2).all?{|a,b| a<b}
    ==>true
strings.each_cons(2).all?{|a,b| a==b}
    ==>true

-- 
Amazon bans book.  After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo

[toc] | [prev] | [next] | [standalone]


#44992

FromDoug Hoffman <glidedog@gmail.com>
Date2016-01-21 09:00 -0500
Message-ID<56a0e495$0$292$14726298@news.sunsite.dk>
In reply to#44982
On 1/21/16 12:53 AM, foxaudioresearch@gmail.com wrote:

> I know some here may not like it but I am attempting to help explain Forth in
> these demonstrations, versus creating "gee whiz" code. (which I can't do)

> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth

I like your code just fine.

I'm lazy and will use library code.
Many problems come down to lists and strings it seems.

-Doug

Works with any ANS Forth

Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
include FMS-SI.f
include FMS-SILib.f

false constant equ
true  constant lt

: lex { s f -- flag }
   s size: 2 < if true exit then
   s size: 1- 0
   do i    s at: @:
      i 1+ s at: @:
      compare f <> if false unloop exit then
   loop true ;

${ aa bb cc } value l1
${ aa aa aa } value l2
${ aa cc bb } value l3
${ aa } value l4

l1 equ lex . 0
l1 lt  lex . -1

l2 equ lex . -1
l2 lt  lex . 0

l3 equ lex . 0
l3 lt  lex . 0

l4 equ lex . -1
l4 lt  lex . -1

[toc] | [prev] | [next] | [standalone]


#45022

From"HAA" <someone@microsoft.com>
Date2016-01-23 00:35 +1100
Message-ID<n7tb60$1q52$1@gioia.aioe.org>
In reply to#44982
foxaudioresearch@gmail.com wrote:
> I have been bad again.
>
> I know some here may not like it but I am attempting to help explain Forth in
> these demonstrations, versus creating "gee whiz" code. (which I can't do)
>
> So the code is dumbed down. This works for well for me given my intelligence.:-)
>
> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth
>
> BF

The only likely sticking point is the definition for  ,"  which assumes WORD's
buffer is at HERE.  (Win32F will fail)

Even systems that have  ,"  can't be counted on (pun) as several follow the
string with a null, alignment, or both.


[toc] | [prev] | [next] | [standalone]


#45034

Fromhumptydumpty <ouatubi@gmail.com>
Date2016-01-22 15:03 -0800
Message-ID<f6ce7c64-eb72-4782-8bba-263be149d234@googlegroups.com>
In reply to#44982
On Thursday, January 21, 2016 at 7:53:35 AM UTC+2, foxaudio...@gmail.com wrote:
> I have been bad again. 
> 
> I know some here may not like it but I am attempting to help explain Forth in
> these demonstrations, versus creating "gee whiz" code. (which I can't do)
> 
> So the code is dumbed down. This works for well for me given my intelligence.:-)
> 
> http://rosettacode.org/wiki/Compare_a_list_of_strings#Forth
> 
> BF
> 
> P.S.
> 
> The poor guy who wrote the Fortran code demonstration wrote multiple paragraphs
> of sob story about missing features in the language. 
> It makes Forth look pretty good.

Hi!

A shot in the dark :)
---
        CHAR ; Constant FS
: $div          ( $dividend . -- $quotient . $remainder . )
        2dup FS scan 2>r r@ - 2r>
;
        DEFER   adverb ( f -- f' )
        :noname noop  ;  Constant SAMESTRING
        :noname -1 <> ;  Constant ASCENDING
        :noname  1 <> ;  Constant DESCENDING
: $list?        ( $strlist . xt-adverb -- f )
        IS adverb
        0 -rot $div
        BEGIN
                dup 1 = IF 2drop 2drop 0= EXIT THEN
                1 /string $div 2>r 2tuck compare adverb
                IF 2drop 2rdrop EXIT THEN
                2r>
        AGAIN
;

1 [IF]
        cr s" test;test;test;"  SAMESTRING  $list? . .s
        cr s" test;test;test;"  ASCENDING   $list? . .s
        cr s" test;test;test;"  DESCENDING  $list? . .s
        cr ." ---"
        cr s" ab;ac;ad;"        SAMESTRING  $list? . .s
        cr s" ab;ac;ad;"        ASCENDING   $list? . .s
        cr s" ab;ac;ad;"        DESCENDING  $list? . .s
        cr ." --- "
        cr s" ad;ab;ac;"        SAMESTRING  $list? . .s
        cr s" ad;ab;ac;"        ASCENDING   $list? . .s
        cr s" ad;ac;ab;"        DESCENDING  $list? . .s
        cr ." ---"
        cr .s cr bye
[THEN]
---

Have a nice day,
humptydumpty

[toc] | [prev] | [next] | [standalone]


#45089

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2016-01-26 15:50 +0000
Message-ID<2016Jan26.165040@mips.complang.tuwien.ac.at>
In reply to#44982
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/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web