How to resolve the algorithm Compare a list of strings step by step in the Icon and Unicon 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 Icon and Unicon 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 Icon and Unicon programming language

Source code in the icon programming language

#
# list-compare.icn
#
link fullimag

procedure main()
   L1 := ["aa"]
   L2 := ["aa", "aa", "aa"]
   L3 := ["", "aa", "ab", "ac"]
   L4 := ["aa", "bb", "cc"]
   L5 := ["cc", "bb", "aa"]

   every L := (L1 | L2 | L3 | L4 | L5) do {
      writes(fullimage(L))
      writes(": equal ")
      writes(if allequal(L) then "true" else "false")
      writes(", ascending ")
      write(if ascending(L) then "true" else "false")
   }
end

# test for all identical
procedure allequal(L)
   if *L < 2 then return
   a := L[1]
   every b := L[2 to *L] do {
      if a ~== b then fail
      a := b
   }
   return
end

# test for strictly ascending
procedure ascending(L)
   if *L < 2 then return
   a := L[1]
   every b := L[2 to *L] do {
      if a >>= b then fail
      a := b
   }
   return
end


  

You may also check:How to resolve the algorithm Mutual recursion step by step in the Elixir programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Tcl programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Raku programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the R programming language
You may also check:How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Common Lisp programming language