home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / v / vsnbl220.zip / ORDER.INC < prev    next >
Text File  |  1991-02-14  |  2KB  |  48 lines

  1. *    ORDER.INC
  2. *
  3. *    Function to sort the characters within a word.  For example,
  4. *        ORDER('PURPLE') returns 'ELPPRU'.
  5. *
  6. *    The collating sequence can be altered by replacing &ALPHABET
  7. *    with a string containing the desired sequence.
  8. *
  9. *    The function can be exercised with the CODE.SNO program:
  10. *        SNOBOL4 CODE
  11. *        ?    SLOAD('ORDER.INC')
  12. *        ?=ORDER('PURPLE')
  13. *        ELPPRU
  14. *        ?=ORDER('987654321')
  15. *        123456789
  16. *
  17. *    The function is useful when strings are used to represent
  18. *    sets, where individual characters are the set elements.
  19. *    Elements can be added to a set by string concatenation, and
  20. *    deleted by pattern matching with null replacement.
  21. *    Set equality and subset checking can use the order function
  22. *    to produce a standard ordering.
  23. *
  24. *    ORDER is also an interesting function because of the clever
  25. *    coding used to implement an insertion sort.  Characters are
  26. *    removed one at a time from the argument, and inserted at the
  27. *    correct point in the output variable, ORDER.
  28. *
  29. *    This is one of the many programs and functions from
  30. *    "Algorithms in SNOBOL4," by James F. Gimpel.
  31. *
  32. *
  33.     DEFINE('ORDER(S)T,HIGHS,S1')
  34.     ORDER_PAT1 = LEN(1) . T
  35.     ORDER_PAT2 = BREAK(*T) REM . HIGHS
  36.     ORDER_PAT3 = (BREAK(*HIGHS) | REM) . S1        :(ORDER_END)
  37.  
  38. *    Get next character from argument
  39. ORDER    S ORDER_PAT1 =                    :F(RETURN)
  40.  
  41. *    Put all characters >= T into HIGHS
  42.     &ALPHABET ORDER_PAT2
  43.  
  44. *    Find first occurence of one of these higher characters,
  45. *    and insert at that point.
  46.     ORDER ORDER_PAT3 = S1 T                :(ORDER)
  47. ORDER_END
  48.