mercredi 27 mai 2015

scala pattern matching dilemma

i have the following program which contains a recursive fucntion with pattern matching. This code works

def randomSelect(num:Int, lst:List[Symbol]):List[Symbol] = (num, lst) match{

     /** traveerse list recursively extracting randomelemnents

    * 

    */

  case (n, l) =>  {

      if (n>0) {
        println("At " + n + " we got:" + l)
        val rnd = getRandom(l.length)
        val item = l(rnd)
        lst(rnd) :: randomSelect(n - 1, l.filter(it => it != item))
      } else List[Symbol]()

  }

}

while the following does not. And i cannot understand why

def randomSelect(num:Int, lst:List[Symbol]):List[Symbol] = (num, lst) match{

     /** traveerse list recursively extracting randomelements

    * 

    */

  case (n, mylist) =>  {
         val rnd = getRandom(mylist.length)
         val item = mylist(rnd)
         lst(rnd) :: randomSelect(n - 1, mylist.filter(it => it != item))

  }
  case (0, mylist) => List[Symbol]()

}

in the second conde snippet, case(0,mylist) is never invoked.

what am i missing?

kind regards marco

Aucun commentaire:

Enregistrer un commentaire