jeudi 25 juin 2015

pattern matching in scala / recursion

I'd like to implement a function that counts the number of same characters directly following. This function always and only starts at the head.

function((Char, Int), List[char]) => ((Char, Int), List[Char])

e.g. (('b',0), List('b','b','x','x')) => (('b', 2), List('x', 'x'))

  def function: ((Char, Int), List[Char]) => ((Char, Int), List[Char])={
    case(('a', b), Nil) => (('a',b), Nil)
    case(('a', b), t::xs)  => (('a', b), t::xs)
    case (('a', b), a::xs) => function(('a', b+1),xs)
  }

I can't find the mistake in my pattern matching.

Are the two characters in the line

case (('**a**', b), **a**::xs) => function(('a', b+1),xs)

the same (like 'a' == 'a'), when I give them the same character?

thanks!

Aucun commentaire:

Enregistrer un commentaire