A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Duplicate Character Counter in Haskell page! Here, you'll find the source code for this program as well as a description of how the program works.
{-# LANGUAGE TupleSections #-}
module Main where
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import System.Environment (getArgs)
duplicateCharacters :: String -> Map Char Int
duplicateCharacters = Map.filter (> 1) . Map.fromListWith (+) . map (, 1)
printDuplicates :: String -> Map Char Int -> IO ()
printDuplicates [] _ = return ()
printDuplicates (x:xs) m =
case Map.updateLookupWithKey (\_ _ -> Nothing) x m of
(Just count, m') -> putStrLn (x : ": " ++ show count) >> printDuplicates xs m'
_ -> printDuplicates xs m
main :: IO ()
main = do
args <- getArgs
case args of
[xs@(_:_)] ->
let m = duplicateCharacters xs
in if Map.null m
then putStrLn "No duplicate characters"
else printDuplicates xs m
_ -> putStrLn "Usage: please provide a string"
Duplicate Character Counter in Haskell was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.