I'm a student at the university and am learning Scala. I've got an exercise which is to make cartesian product using pattern matching and not using any operations on list.
def find[A,B](aList: List[A], bList: List[B]): Set[(A,B)] = {
def findHelper[A,B](aList: List[A], bList: List[B], acc: Set[(A,B)]): Set[(A,B)] = {
(aList, bList) match {
case (head1 :: tail1, head2::tail2) => {
findHelper[A, B](tail1, tail2, acc ++ Set((head1, head2)))
}
case (List(), head2::tail2) => acc
case (List(), List()) => acc
}
}
findHelper(aList, bList, Set())
}
println(find(List(1,2,3,4,5), List(7,8,9,10,11)))
As a result I get only like (1,7), (2,8)
etc. I obviously know why, but I don't know how to combine each pair with itself. I don't know what to do, when my first list gets empty after ::
operation.
Aucun commentaire:
Enregistrer un commentaire