I would like to execute a list of functions over a list of corresponding values:
let f1 x = x*2;;
let f2 x = x+70;;
let conslist = [f1;f2];;
let pmap2 list1 list2 =
seq { for i in 0..1 do yield async { return list1.[i] list2.[i] } }
|> Async.Parallel
|> Async.RunSynchronously;;
Result:
seq { for i in 0..1 do yield async { return list1.[i] list2.[i] } }
----------------------------------------------^^^^^^^^^
stdin(213,49): error FS0752: The operator 'expr.[idx]' has been used an object of indeterminate type based on information prior to this program point. Consider adding further type constraints
I would like to execute: pmap2 conslist [5;8];; (in parallel)
As the error message suggests, you need to add type annotations to
list1
andlist2
. Once you do that, it works fine (though I would recommend using arrays instead of list since you're random-accessing them).If you want to use random access then you should use arrays. Random access to elements of list will work, but it is inefficient (it needs to iterate over the list from the start). A version using arrays would look like this:
However, you can also rewrite the example to work with any sequences (including arrays and lists) using the
Seq.zip
function. I think this solution is more elegant and it doesn't force you to use imperative arrays (and it doesn't have the performance disadvantage):