reset password
Author Message
cydneyauman
Posts: 12
Posted 22:54 Oct 31, 2012 |

I used the hint for combinations that was posted on the wiki much earlier in the week. 

I'm getting an error on the line 1 <- 1 to n. 

This type of for expression was discussed in this weeks lectures, does it not work on a list of lists?

 

    def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match {
        // for the Nil case return the empty list of lists
        case Nil => List(List())
       
        case (c, n) :: tail => {
         
        // find the combinations of the tail
          val subcombinations: List[Occurrences] = combinations(tail)
       
        //For each value i in 1 to n
        // add (c, i) to each occurrence list in subcombinations.
        // That's why addHead is of the type List[List[Occurrences]]
        // Flatten addHead and add those to subcombinations


          val addHead: List[List[Occurrences]] = for {
            i <- 1 to n
          }yield (c, i) :: subcombinations
         
            addHead.flatten
        }
    }

rabbott
Posts: 1649
Posted 23:11 Oct 31, 2012 |

Are you talking about this part:

 

val addHead: List[List[Occurrences]] = 
     for {
            i <- 1 to n
          } yield (c, i) :: subcombinations
 
 

subcombinations is of type List[Occurrences]

Does it really make sense to attach (c, i) to the front of such a list? You probably want to attach (c, i) to each element in that list, since each element is of type Occurrences.

Last edited by rabbott at 09:26 Nov 01, 2012.