Question
Using pattern matching, write a function using SCALA that computes for each unique character in the list `chars` the number of times it occurs: def
Using pattern matching, write a function using SCALA that computes for each unique character in the list `chars` the number of times it occurs: def times(chars: List[Char]): List[(Char, Int)] = ??? For example, the invocation times(List('a', 'b', 'a')) should return the following (the order of the resulting list is not important): List(('a', 2), ('b', 1)) Help on Pairs The type `List[(Char, Int)]` denotes a list of pairs, where each pair consists of a character and an integer. Pairs can be constructed easily using parentheses: val pair: (Char, Int) = ('c', 1) In order to access the two elements of a pair, you can use the accessors `_1` and `_2`: val theChar = pair._1 val theInt = pair._2 Another way to deconstruct a pair is using pattern matching: pair match { case (theChar, theInt) => println("character is: "+ theChar) println("integer is : "+ theInt) }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started