Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions