Multiplication function of matrices represented as list [list [int]]

Please, help me to write multiplication function of matrices represented as list [list [int]] (Scala)

What have you tried? Why didn’t it work?

Advice, check the Scaladoc, especially look to transponse, zip, map

I have zip and map functions that work but the transpose function doesn’t work and I don’t know what is the reason

def zip[B](list: List[B]): List[(A,B)] = {
def go(xs: List[A], ys: List[B]): List[(A,B)] = (xs, ys) match {
case (List.Nil, ) | (, List.Nil) => List.Nil
case (List.Cons(xh, xt), List.Cons(yh, yt)) =>
List.Cons((xh, yh), go(xt, yt))

def map[B](f: (A) => B): List[B] = {
def go(xs: List[A], acc: List[B]): List[B] = {
xs match {
case List.Nil => acc
case List.Cons(xh, xt) => List.Cons(f(xh), go(xt, acc))

def transpose[A] (l: List[List[A]]): List[List[A]] = {
def go(ls: List[List[A]], acc: List[List[A]]): List[List[A]] = ls match {
case Nil => acc
case Cons(head,tail) => go(tail,

“Doesn’t work” — do you get a compile-time error, a runtime error, or some other unexpected behavior? If there is an error message, please include it. (This advice applies 100% of the time when asking programming questions on the internet.)

1 Like

Found: (head : Option[T])
Required: List[Option[T]]

So it seems you are not using the stdlib List but rather your own implementation? You should have stated that first since it changes a lot the approach to solve this problem.

transponse is somewhat complex to implement and if you are going to solve this problem using tail-recursion, then you don’t need it for the matrix multiplication.

Yes, it is my own implementation of List. Sorry, but can you tell me how can I write this function. And how can I solve problem with transpose because I need to write it too.

Okay then, if you also need transpose then yeah, fix that first and then you can use it to solve this problem.

But can you tell me, please, how I can write it without using transpose too.

You can write it by trying to write it yourself, and if you get stuck, show us your best effort and explain where you got stuck and why, and we’ll do our best to help you get unstuck.

Basically, by inlining all that those functions do in a single big method that in structure will be very similar to transpose