Functional style for multiplying two lists with Scala

I am using Scala 2.9 and would like to build a list based on some operations.

Consider the following: I have two simple lists:

    val l1 = List (2,3)
    val l2 = List (List (4,5,6), List (7,8,9))

The behavior I want is as follows: operation with both lists, for example:

    (2 * 4) + (3 * 7)
    (2 * 5) + (3 * 8)
    (2 * 6) + (3 * 9)

And I would like to have as a result another list with these values:

29.34.39

I am already trying to solve the source code above. I probably think about it completely wrong, but I try my best to come up with an elegant solution.

    val lr = (l1, l2) .zipped.map ((t1: Int, t2: List [Int]) =>
        ...
    )    
    println (lr) // should print List (29, 34, 39)

, Im , . - ?

+5
1

, , :

l2.transpose.map(sl => (l1, sl).zipped.map{ case(x,y) => x*y }.sum)
res: List[Int] = List(29, 34, 39)

@Tharabas @michael_s, , , :

l2.transpose.map((l1,_).zipped.map(_*_).sum)
+12

All Articles