Created
July 31, 2012 00:13
-
-
Save SamirTalwar/3212228 to your computer and use it in GitHub Desktop.
Detecting whether two strings are anagrams, in Haskell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Didn't expect such a quick response, but -- AAAAAHHHH So that's how you map a function, I tried
map toLowerbut actually wasn't placing the parentheses properly; so my newb self just applied an extra function.Thank you for the response and explanation!