In Haskell
How to write a function that receives a list and returns a list of tuples that contains the character and number of occurrence on the list.
for example: "Hello" -> [ ('H',1), ('e',1), ('l',2),('l',2),('o',1) ]
Haskell code:
import Data.List
countOccurs :: String -> [(Char, Int)]
countOccurs str = map (\x -> (head x, length x)) $ group $ sort str
main = do
str <- getLine
print $ countOccurs str
Haskell code screenshot:
Output:
Get Answers For Free
Most questions answered within 1 hours.