omega
omega

Reputation: 43843

How to use map with two lists?

In SML, how can you use a map function that takes two lists and returns a list of each element in one list multiplied with its corresponding element in the other list?

I tried

fun mult_list v1 v2 = map (fn (x, y) => x * y) (v1, v2);

but it's not working...

Upvotes: 0

Views: 1253

Answers (1)

Andreas Rossberg
Andreas Rossberg

Reputation: 36098

You want ListPair.map:

fun mult_list v1 v2 = ListPair.map (fn (x, y) => x * y) (v1, v2)

which, btw, can be abbreviated to

fun mult_list v1 v2 = ListPair.map op* (v1, v2)

You can also use ListPair.mapEq instead, if you want to force an exception when the lists are not of the same length.

Upvotes: 3

Related Questions