Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In F#, how does map2 handle uneven list lengths?

How does the map2 function handle uneven length lists?

e.g.

let list1 = [1; 2; 3]
let list2 = [4; 5; 6; 7]
let sumList = List.map2 (fun x y -> x + y) list1 list2
printfn "%A" sumList

It seems odd that this isn't mentioned in the documentation.

like image 869
sdgfsdh Avatar asked Dec 04 '25 02:12

sdgfsdh


1 Answers

Even though the documentation doesn't cite it, one can easily check the source code to find out what happens and the implementation details.

As you can see in the link, when you pass a list of different size, invalidArg is called, throwing an ArgumentException with the message stored in the listsHadDifferentLengths resource.

like image 73
William Barbosa Avatar answered Dec 06 '25 07:12

William Barbosa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!