
A World Without Strings Is Chaos - lelf
http://beyondloom.com/blog/strings.html
======
Thiez
I wonder how many of the solutions would still work for arbitrary unicode
strings. It looks like the author assumes only ascii exists. Perhaps the
interns would be better off if they were taught that "reverse a string" is a
fundamentally dangerous (and pointless) operation in almost any real-life
context. Strings are way more complex than people give them credit for.

~~~
togs
It’s not clear to me why reversing a string is dangerous or pointless.

~~~
bastawhiz
An array of characters reversed loses the original meaning of the characters.
Other than looking for palindromes, there's almost nothing in the way of
practical uses of a reversed string that can't be accomplished by subscripting
the string and iterating from the end to the beginning.

~~~
catlifeonmars
I found this little gem on Software engineering StackExchange:
[https://softwareengineering.stackexchange.com/questions/2469...](https://softwareengineering.stackexchange.com/questions/24691/what-
do-you-use-string-reversal-for)

Suffice it to say, there are many interesting uses for string reversal :)

~~~
bastawhiz
Interesting, but questionably useful. If you're converting integer bases or
obfuscating email addresses, there are much better approaches that don't
involve a bad hack

------
jodrellblank
Dyalog APL:

    
    
        0 - Multiplicity (count character)
        'h'(+/=)'fhqwhgads'
    
        1 - Trapeze Part (palindrome)
        (⊢≡⌽)'racecar'
    
        2 - chars which appear more than once
            (not too happy with my Key ⌸ ones)
        {k←{⍺,⍨1<≢⍵}⌸⍵ ⋄ k[;1]/k[;2]} 'applause'
    
        3 - reordered letters
        'teapot'{⍺[⍋⍺]≡⍵[⍋⍵]}'toptea'
        ≡/(⊂∘⍋⌷⊢)¨'teapot' 'toptea'
    
        4 - chars which appear once
        {k←{⍺,⍨1=≢⍵}⌸⍵ ⋄ k[;1]/k[;2]} 'somewhat heterogenous'
    
        5 - musical chars
        'barfoo' {∨/⍺≡⍤1⊢⍵⌽⍤0 1⍨⍳≢⍵} 'foobar'
    
        6 - sort strings by length, ascending
        {⍵[⍋≢¨⍵]} 'books' 'apple' 'peanut' 'aardvark' 'melon' 'pie'
        ((⊂∘⍋(≢¨⊢))⌷⊢) 'books' 'apple' 'peanut' 'aardvark' 'melon' 'pie'
    
        7 - Most frequent character
        {k←{⍺,≢⍵}⌸⍵ ⋄ ⊃k[;1][⍋k[;2]]} 'abdbbac'
    
        8 - reverse words
        {⊃{⍺,' ',⍵}/⌽¨' '((~=)⊆⊢)⍵} 'a few words in a sentence'
    
        9 - compress
        1 0 0 1 0 1/'foobar'
    
        10 - expand
        1 0 0 1 0 1 {'_'@(⍸~⍺)⍺\⍵} 'fbr'
    
        11 - Consonants
        {'_'@(⍸'AEIOUYaeiouy'∊⍨⍵)⊢⍵} 'FLAPJACKS'
    
        12 - Consonants Rdx
        {⍵/⍨~'AEIOUYaeiouy'∊⍨⍵} 'FLAPJACKS'
    
        13 - Word replace
        ⍸'a few words in a sentence' {idx←⍸⍵⍷⍺ ⋄ idx,←idx+¨⍳¯1+≢⍵ ⋄ 'x'@idx⊢⍺} 'words'
    
        14 - Permutations
        ? a recursive one shouldn't be so hard, but..
        {0=≢⍵:'' ⋄ ... } 'xyz'
    
        non-recursive, much harder:
        https://code.jsoftware.com/wiki/Doc/Articles/Play202

~~~
xelxebar
Nice! Here are some J solutions. Unfortunately, I only have time for a few,
right now. Hopefully, I can add some more later:

    
    
        'mississippi' +/@:= 's'         NB. 0 - Multiplicity
        (-:|.) 'racecar'                NB. 1 - Trapeze Part
        (~. #~ 1 < +/@|:@=) 'applause'  NB. 2 - Duplicity
        'teapot' -:&({~/:) 'toptea'     NB. 3 - Sort Yourself Out
        (~. #~ 1 = +/@|:@=) 'foo bar'   NB. 4 - Precious Snowflakes

~~~
xelxebar
Just for posterity, here are the rest I came up with:

    
    
        'foobar' (e. ] A.~ +/@(!@i.@- *&|: >:@i.@- $&> i.)@#) 'barfoo'  NB. 5 - Musical Chars
        (/: #&>) 'books';'apple';'peanut';'aardvark';'melon';'pie'      NB. 6 - Size Matters
        (~. #~ [: (= >./) +/@|:@=) 'abdbbac'                            NB. 7 - Popularity Contest
        |.&.>&.;: 'a few words in a sentence'                           NB. 8 - esreveR A ecnetneS
        'foobar' #~ 1 0 0 1 0 1                                         NB. 9 - Compression Session
        'fbr' [`(I.@])`($&'_'@#@])} 1 0 0 1 0 1                         NB. 10 - Expansion Mansion
        '_'&(I.@e.&'AIUEOaiueo'@]}) 'FLAPJACKS'                         NB. 11 - C_ns_n_nts
        -.&'AIUEOaiueo' 'FLAPJACKS'                                     NB. 12 - Cnsnnts Rdx
        'one fish two fish' [`($&'X'@#&.>@[)@.e."0 _&.;: 'fish'         NB. 13 - TITLE REDACTED
        (A.~ i.@!@#) 'xyz'                                              NB. 14 - It's More Fun to Permute
    

Note that 14 is trivially non-recursive. I am fairly happy with these
solutions, especially 5 and 13 which took the most thought.

------
kazinator

      This is the TXR Lisp interactive listener of TXR 233.
      Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
      1> (countq #\h "fhqwhgads")
      2
      2> [[callf equal identity reverse] "palindrome"]
      nil
      3> [[callf equal identity reverse] "racecar"]
      t
      4> [(opip (mappend [iff (op > (countq @1 @@1) 1) list] @1) uniq) "applause"]
      "ap"
      5> [(opip (mappend [iff (op > (countq @1 @@1) 1) list] @1) uniq) "foo"]
      "o"
      6> [(opip (mappend [iff (op > (countq @1 @@1) 1) list] @1) uniq) "baz"]
      ""
      7> [[mapf equal sort sort] "teapot" "toptea"]
      t
      8> [[mapf equal identity sort] "apple" "elap"]
      nil
      9> [(op mappend [iff (op eql (countq @1 @@1) 1) list] @1) "somewhat heterogeneous"]
      "mwa rgnu"
      10> [(do and (= (len @1) (len @2)) (search-str `@2@2` @1)) "foobar" "barfoo"]
      3
      11> [(do and (= (len @1) (len @2)) (search-str `@2@2` @1)) "fboaro" "foobar"]
      nil
      12> [sort '#"books apple peanut aardvark melon pie" : len]
      ("pie" "books" "apple" "melon" "peanut" "aardvark")
    

Yawn ...

    
    
      13> (perm "xyz")  ;; non-recursive, lazy, written in C.
      ("xyz" "xzy" "yxz" "yzx" "zxy" "zyx")

~~~
kazinator
Erratum:

    
    
      8> [[mapf equal identity sort] "apple" "elap"]
      nil
    

Should be:

    
    
      ... sort sort ...

------
perl4ever
I was thinking I really need to research how to efficiently deal with strings
in a certain language that doesn't allow them to be accessed as arrays of
characters. Because then I could get started on porting some C code to it.

~~~
swsieber
Ha ha. Rust? I suppose it depends on the context of what you're trying to do.

Edit: if it is Rust most of the time you'll want to use character
iterators/map/filter/etc. Then benchmark.

~~~
trevyn
The Rust docs have good sections on this:

[https://doc.rust-lang.org/book/ch08-02-strings.html](https://doc.rust-
lang.org/book/ch08-02-strings.html)

[https://doc.rust-lang.org/std/primitive.char.html](https://doc.rust-
lang.org/std/primitive.char.html)

TLDR you _can_ straightforwardly access as bytes, or iterate as "Unicode
scalar values" as stored in the 4-byte "char" type.

(But a "Unicode scalar value" is not exactly equal to the colloquial meaning
of "character".)

However: If you care about valid UTF-8 inputs not breaking the function or
intent of your code, it's probably time to understand the problems Unicode is
actually trying to address, and apply those considerations to what you're
trying to do. :)

------
enriquto
I hate string processing. Is there any widely used programming language that
_does not_ support strings? I mean, you can do all of math, scientific
computing, and graphics without strings. Forcing your language to support
strings will inevitably impose compromises that make the language worse when
you do not need them. I want such a language, unencumbered by the intrinsic
uglyness of strings.

~~~
bryal
Futhark has only the most basic string support -- string literal syntax, which
which is just sugar for a utf-8 byte array. It's a functional array language
that generates fast GPU code, and as a GPU program is essentially a pure
function (no input/output), string handling is very much a secondary concern.
Futhark is used exactly for math, scientific computing, and graphics, and not
for strings.

------
Tokkemon
That is an absolutely delightful Mouse Hunt reference I wasn't expecting
today.

------
rambojazz
Can somebody explain the title to me? This is just a collection of puzzles.

~~~
MaxBarraclough
It's a reference to a quote from the 1997 film _Mouse Hunt_.

[https://en.wikiquote.org/wiki/MouseHunt_(film)#Lars_Smuntz](https://en.wikiquote.org/wiki/MouseHunt_\(film\)#Lars_Smuntz)

------
m4r35n357
Physicists will be disappointed ;)

~~~
symplee
And orchestras...

~~~
tartoran
And solo guitarists too.

~~~
soneca
And puppeteers

~~~
tartoran
Oh, I forgot about politics..

