Question

In Haskell How to write a function that receives a list and returns a list of...

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) ]

Homework Answers

Answer #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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a recursive function elemAt that returns the ith item of the list, where the first...
Write a recursive function elemAt that returns the ith item of the list, where the first item is index 0. You may not use any of the standard Haskell functions that operate on lists. For Example: List is [5,6,7,78,8,7]          Element at index 3 is 78
10. Write a function in HASKELL howManyBelowAverage that returns how many of three integer inputs are...
10. Write a function in HASKELL howManyBelowAverage that returns how many of three integer inputs are below its average value. (Hint: utilize the averageThree function.) howManyBelowAverage :: Integer -> Integer -> Integer -> Integer averageThree :: Integer -> Integer -> Integer -> Float AverageThree x y z = fromInteger(x+y+z) / 3.0 Can someone please help me solve the howManyBelowAverage function?
In Python: Problem 5] Write a function that accepts a list as argument and returns a...
In Python: Problem 5] Write a function that accepts a list as argument and returns a list that contains: (1) The positive elements of the taken list (2) The negative elements of the taken list (3) The odd elements of the taken list (4) The even elements of the taken list
C++: Write a function that receives a pointer to a character string consisting of only alphabets...
C++: Write a function that receives a pointer to a character string consisting of only alphabets a-z and returns the character with the maximum number of repetitions. If more than one-character repeats same number of times, return the character with smallest alphabetical order. For example, the string “Mississippi” has three repeated characters (i-4, s-4, p-2). Both ‘i’ and ‘s’ repeated 4 times but ‘i’ is alphabetically smaller hence the function should return ‘i’. Do not count repeated blanks, special characters,...
Write a Haskell program that generates the list of all the subsets of the set [1..n]...
Write a Haskell program that generates the list of all the subsets of the set [1..n] that have as many elements as their complements. Note: the complement of a set contains all the elements in [1..n] that are not members of the given set. Show the outputs for n=6.
Function name : matrixMultiplication Parameters : aMatrix (list), bMatrix (list) Returns Type: list of tuples Description...
Function name : matrixMultiplication Parameters : aMatrix (list), bMatrix (list) Returns Type: list of tuples Description : Write a function in PYTHON that takes in two matrices (list of tuples or list of lists) and multiplies the two matrices together. Assume that the given matrices will be valid, i.e. aMatrix will be n x m (n rows and m columns) and bMatrix will be m x ℓ (m rows and ℓ columns). *Assume elements of aMatrix & bMatrix will be...
Define a function in Scheme which takes a list of numbers and a scalar and returns...
Define a function in Scheme which takes a list of numbers and a scalar and returns the rest of the list after the first occurrence of the scalar that was passed. An empty list should be returned if the value doesn't exist. For example, (list-past-scalar '(5 2 8 3 1 9 2 3) 3) #returns (1 9 2 3) (list-past-scalar '(37 18 38 65 90) 100) #returns ()
Python pls create a function called search_position. This function returns a list of 2 tuples and...
Python pls create a function called search_position. This function returns a list of 2 tuples and the number should be start highest number. The first index is the number, and second are list of 2 tuples that sorted by position in alphabetical order: The first index will be position and second index will be champion's name(This also should be sorted by alphabetical order). team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
In Python, write a function that takes in an arbitrary list of numeric triples (tuples of...
In Python, write a function that takes in an arbitrary list of numeric triples (tuples of size 3), and returns a new list containing the maximum elements from each
Please do in Haskell programming language. Write a function intLog of type Integer -> Integer that...
Please do in Haskell programming language. Write a function intLog of type Integer -> Integer that returns the exponent of the largest power of 2 less than its integer argument. Your function need not behave well if the parameter is negative. Try your function on intLog 468.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT