title: Remove characters from String in Haskell
body: <p>I am creating a program that reads a text file and splits up words and stores them in a list. I have been trying to create a function that takes in a String which is the whole text String from the file and remove punctuation e.g. ";", ",", "." but unfortunately haven't had any luck yet. The program works without the punctuation function, but not when I include it to <code>(toWords fileContents)</code> Please can someone look at what I have done and see what I am doing wrong.</p>
  
  <p>Here is the code that I have so far:</p>
  
  <pre><code>main = do  
         contents &lt;- readFile "LargeTextFile.txt"
         let lowContents = map toLower contents
         let outStr = countWords (lowContents)
         let finalStr = sortOccurrences (outStr)
         let reversedStr = reverse finalStr
         putStrLn "Word | Occurrence "
         mapM_ (printList) reversedStr
  
  -- Counts all the words.
  countWords :: String -&gt; [(String, Int)]
  countWords fileContents = countOccurrences (toWords (removePunc fileContents))
  
  -- Splits words and removes linking words.
  toWords :: String -&gt; [String]
  toWords s = filter (\w -&gt; w `notElem` ["an","the","for"]) (words s)
  
  -- Remove punctuation from text String.
  removePunc :: String -&gt; String
  removePunc xs = x | x &lt;- xs, not (x `elem` ",.?!-:;\"\'")
  
  -- Counts, how often each string in the given list appears.
  countOccurrences :: [String] -&gt; [(String, Int)]
  countOccurrences xs = map (\xs -&gt; (head xs, length xs)) . group . sort $ xs
  
  -- Sort list in order of occurrences.
  sortOccurrences :: [(String, Int)] -&gt; [(String, Int)]
  sortOccurrences sort = sortBy (comparing snd) sort
  
  -- Prints the list in a format.
  printList a = putStrLn((fst a) ++ " | " ++ (show $ snd a))
  </code></pre>