tags:

views:

57

answers:

2

I'm trying to subtract 2 lists and return the compared product.

So if list a = [2,3,2] b = [1,1,1] then a-b = [1,2,1] and the returned product (c) should be 1.

val c = List.map (fn i => (i - b) mod 10) a

modulo (mod) 10 is for cases where the two subtracted numbers gives an odd result, e.g. 2-8 = ~6 mod 10 = 4.

I'm stuck at the subtraction, because List.map doesn't allow me to do the subtraction because it expects an int value and not an int list (at least not the way I have coded it :( ).

I'm also blank on the comparison.

+1  A: 

You don't want to subtract b — you want to subtract the corresponding value. A convenient way to do this is to zip the lists together:

val c = List.map (fn (i, j) => (i - j) mod 10) (ListPair.zip (a, b))
Chuck
+2  A: 

You can use ListPair.map to iterate over 2 lists at once, like this:

val c = ListPair.map (fn (i, j) => (i - j) mod 10) (a,b)
sepp2k