re: Write a script to identify an anagram VIEW POST

TOP OF THREAD FULL DISCUSSION
re: I like the solution to "subtract" all characters of the shorter word from the (possibly) longer word, and check if there is some character left: ...
 

I'm sorry, it took me a dinner out to see, this was unnecessarily complicated. :) The delete functions perfectly fits a right fold. And if the words aren't of equal length, they are not anagrams.

-- deletes the first occurance of element x from a list
delete _ [] = []
delete x (y:ys) = if x == y then ys else y : delete x ys

-- deletes all elements of the second list from the first
without :: Eq a => [a] -> [a] -> [a]
without = foldr delete

-- are there any remaining characters? 
isAnagram word1 word2 = null $ word1 `without` word2

delete could also be imported from Data.List

code of conduct - report abuse