Archive for February, 2012

Programming Praxis – Remove characters from a string

February 24, 2012

In today’s Programming Praxis exercise, our task is to remove all the characters in one string from the other. Let’s get started, shall we?

Since this exercise doesn’t mention any performance requirements, we’ll go with the short but naive version, which is O(m * n). If the strings used get larger, you’ll probably want to use different datastructures to have more efficient membership testing, such as a hastable which reduces the complexity to O(m).

remove :: Eq a => [a] -> [a] -> [a]
remove = filter . flip notElem

A quick test to see if it’s working properly:

main = print $ remove "aeiou" "Bonsai Code"