Skip to content

Instantly share code, notes, and snippets.

@paulcc
Created April 15, 2013 10:28
Show Gist options
  • Save paulcc/5387193 to your computer and use it in GitHub Desktop.
Save paulcc/5387193 to your computer and use it in GitHub Desktop.
import Data.List(nub, sortBy, groupBy)
import Data.Char(isAlpha, toLower)
isograms s
= [ (length w, w)
| w <- nub $ words $ map toLower s
, all isAlpha w
, nub w == w ]
main = do text <- readFile "pg11.txt"
print $ maximum $ isograms text
@paulcc
Copy link
Author

paulcc commented Apr 15, 2013

This is in Haskell.

Similar structure to the Ruby version, but arguably "cleaner" and it also exploits laziness to avoid calculating everything so memory footprint is (probably) lower. For example, when it's found a length N word then it will discard words which are shorter, and stops the isogram test as soon as a repeat char is found.

Code basically says, for all unique lower-case words in the input, select the words which are all alphanumeric and pass the isogram test, then tag each word with its length. Then take the maximum of the isogram list.

There's various possible improvements possible, if required.

It's also type safe too, ie no strange crashes and no run-time overheads to check that it isn't mixing numbers and strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment