Reputation: 241
So i am trying to multiply the nth element of one list with the nth element of another list and add them together
let listMulti xList yList =
|> [for x in xList do
for y in yList do
yield x*y ] // multiplies all the elements on x with y
|> List.filter(fun x-> List.nth % List.length (xList) =1 ) //gets the elements 1 , 4, 7 for a list of size 3. This is scalable
|> List.sum //add them all up
So the idea here is
>listMulti [1;2;3][4;5;6];;
val it : int = 32
So 1*4 + 2 *5 + 3*6 = 32 ,but instead im getting
error FS0010: Unexpected infix operator in binding
Help?
Upvotes: 2
Views: 1156
Reputation: 25516
The error is because you are using List.nth
in a weird way.
I would do something like
List.zip xlist ylist
|> List.sumBy (fun (a,b) -> a*b)
Here list.zip
combines the lists - so if you had [1;2;3]
and [4;5;6]
you get [(1,4);(2,5);(3,6)]
. Then you just multiply and sum in one go.
Upvotes: 3