Anagramatically Aehklls

Posted on September 26, 2016

I just wanted to play fun at someone I sent a text to, and I started by manually reversing individual words. This quickly turned out to be too tedious for me, however, so I launched a some_shell and didn’t remember how to do a reverse there. I looked around a bit but quickly gave up. Then I remembered I could just use stack to get a ghci shell, so I just issued

reverse "My sentence here"

To reverse individual words, I modified that slightly to:

unwords . map reverse . words

There’s a schema here. Let’s create a small function:

let wordly wise = unwords . map wise . words

Okay. Now to do it anagramatically:

import Data.List (sort)
let anagramatically = wordly sort

Now I can do:

anagramatically "Hello World"

to get:

"Hello Wdlor"

One could further improve this to capitalise the first letter and otherwise ignore the case when sorting. One could always further improve things :-)

Another fun thing to do is build palindromes, just because we can:

import Control.Arrow ((&&&))
let palit = wordly (uncurry (++) . (id &&& reverse))

For example, this sentence would look like this:

putStrLn $ palit "For example, this sentence would look like this:"
ForroF example,,elpmaxe thissiht sentenceecnetnes woulddluow lookkool likeekil this::siht

On an unrelated note, I’m really looking forward to haskell eXchange next week…