How to resolve the algorithm Compare a list of strings step by step in the Forth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Compare a list of strings step by step in the Forth programming language

Table of Contents

Problem Statement

Given a   list   of arbitrarily many strings, show how to:

Each of those two tests should result in a single true or false value, which could be used as the condition of an   if   statement or similar. If the input list has less than two elements, the tests should always return true. There is no need to provide a complete program and output. Assume that the strings are already stored in an array/list/sequence/tuple variable (whatever is most idiomatic) with the name   strings,   and just show the expressions for performing those two tests on it (plus of course any includes and custom functions etc. that it needs),   with as little distractions as possible. Try to write your solution in a way that does not modify the original list,   but if it does then please add a note to make that clear to readers. If you need further guidance/clarification,   see #Perl and #Python for solutions that use implicit short-circuiting loops,   and #Raku for a solution that gets away with simply using a built-in language feature.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Compare a list of strings step by step in the Forth programming language

Source code in the forth programming language

\ linked list of strings creators
: ,"       ( -- )  [CHAR] " WORD  c@ 1+ ALLOT  ;             \ Parse input stream until " and write into next available memory
: [[       ( -- )  0 C, ;                                    \ begin a list. write a 0 into next memory byte (null string)
: ]]       ( -- )  [[ ;                                      \ end list with same null string

: nth      ( n list -- addr) swap 0 do count + loop ;        \ return address of the Nth item in a list

: items    ( list -- n )                                     \ return the number of items in a list
          0 >R
          BEGIN
            COUNT + DUP
            R> 1+ >R
          0= UNTIL
          DROP
          R> 1- ;

: compare$ ( $1 $2 -- -n|0|n )  count rot count compare ;    \ compare is an ANS Forth word. returns 0 if $1=$2

: compare[]   ( list n1 n2 -- flag)                          \ compare items n1 and n2 in list
            ROT dup >R nth ( -- $1)
            swap r> nth    ( -- $1 $2)
            compare$ ;

\ create our lexical operators
: LEX=     ( list -- flag)
           0                                                 \ place holder for the flag
           over items 1
           DO
              over I  I 1+ compare[] +                       \ we sum the comparison results on the stack
           LOOP
           nip 0= ;

: LEX<     ( list -- flag)
           0                                                 \ place holder for the flag
           over items 1
           DO
              over I  I 1+ compare[] 0< NOT +
           LOOP
           nip 0= ;

\ make some lists
create strings  [[ ," ENTRY 4" ," ENTRY 3" ," ENTRY 2" ," ENTRY 1" ]]
create strings2 [[ ," the same" ," the same" ," the same" ]]
create strings3 [[ ," AAA" ," BBB" ," CCC" ," DDD" ]]


: test-equality ( string node -- new-string bad? ) 
    over count                          \ -- string node adr cnt 
    rot .line @ count   compare ; 

: test-ascending ( string node -- new-string bad? ) 
    .line @ >r 
    count  r@ count     compare -1 <>   \ -- bad? 
    r> swap ; 

: test-seq { seq 'test -- flag }        \ 'TEST picture: string node -- new-string bad? 
     seq length 2 < if  true exit then 
     seq .line @  seq 2nd  'test  find-node 
     nip  0= ;


  

You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the Python programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Narcissist step by step in the VBA programming language
You may also check:How to resolve the algorithm Repeat step by step in the Racket programming language
You may also check:How to resolve the algorithm FASTA format step by step in the XPL0 programming language