Error FS0752 in F# declaration of a map over list of functions

点点圈 提交于 2019-12-23 12:08:54

问题


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)


回答1:


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:

// Needs to be declared as array
let conslist = [|f1; f2|];; 

// Add type annotations to specify that arguments are arrays
let pmap2 (arr1:_[]) (arr2:_[]) = 
  seq { for i in 0 .. 1 do 
          yield async { return arr1.[i] arr2.[i] } }
  |> Async.Parallel |> Async.RunSynchronously

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):

// Works with any sequence type (array, list, etc.)
let pmap2 functions arguments = 
  seq { for f, arg in Seq.zip functions arguments do 
          yield async { return f arg } }
  |> Async.Parallel |> Async.RunSynchronously



回答2:


As the error message suggests, you need to add type annotations to list1 and list2. Once you do that, it works fine (though I would recommend using arrays instead of list since you're random-accessing them).




回答3:


let pmap2 (list1:_ list) (list2:_ list)


来源:https://stackoverflow.com/questions/4843445/error-fs0752-in-f-declaration-of-a-map-over-list-of-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!