1

I am blocked since yesterday about a type mismatch error and I don't see how to correct it. Maybe you can help me with it.

def combine( head : (Char,Int), xs : Occurrences) : List[Occurrences] =
 xs.map { case (x,i) => for ( occu <- 1 to head._2 ) yield List((x,i), (head._1, occu)) }

Here is the error that I get :

type mismatch; 
found :   List[scala.collection.immutable.IndexedSeq[List[(Char, Int)]]]   
required: List[forcomp.Anagrams.Occurrences]

The type Occurrences is defined as type Occurrences = List[(Char, Int)]

How can I fix this error?

2 Answers 2

4

You could solve your problem by using flatMap which will concatenate (flatten) the Lists for you.

def combine( head : (Char,Int), xs : Occurrences) : List[Occurrences] =
  xs.flatMap { case (x,i) => (1 to head._2).map(occu =>List((x,i), (head._1, occu))) }

Now for each occurance it'll produce a list that has the (x,i) tuple and the (head._1, occu) tuple and all of the lists will essentially be ++'d together by the flatMap.

Note that I'm blindly converting your code since I know this is homework so I won't attempt to analyze if the algorithm is correct.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't worry, based on your answer, I finally find a solution. Thanks
1

The problem is that for each member of Occurrences you yield a List – so you get something like a List[List[Occurrences]]. I guess you might use flatMap instead of map which will flatten the list.

1 Comment

Thanks for your answer. I forgot flatMap !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.