Skip to content

Instantly share code, notes, and snippets.

@SamirTalwar
Created July 31, 2012 00:13
Show Gist options
  • Select an option

  • Save SamirTalwar/3212228 to your computer and use it in GitHub Desktop.

Select an option

Save SamirTalwar/3212228 to your computer and use it in GitHub Desktop.
Detecting whether two strings are anagrams, in Haskell.
import Data.List
import Test.QuickCheck
anagrams :: String -> String -> Bool
anagrams x y = sort x == sort y
main = do
quickCheck (\(x, y) -> length x <= 8 ==> anagrams x y == (y `elem` permutations x))
-- tests `anagrams` using 100 sets of 2 random strings
-- uses an obvious but painfully slow algorithm
-- discards any test string longer than 8 characters as generating the permutations takes forever
@indiscrete-mathematics

Copy link
Copy Markdown

Didn't expect such a quick response, but -- AAAAAHHHH So that's how you map a function, I tried map toLower but actually wasn't placing the parentheses properly; so my newb self just applied an extra function.

Thank you for the response and explanation!

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