Error FS0752 in the declaration of the F # card above the list of functions

I would like to execute a list of functions on a list of relevant 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: operator 'expr. [idx] 'an object of an undefined type was used, based on information prior to this program point. Consider adding the following type of Constraint

I would like to execute: pmap2 conslist [5; 8];; (parallel)

+3
source share
3 answers

If you want to use random access, you must use arrays. Random access to list items will work, but it is inefficient (it needs to iterate over the list from the very beginning). The version using arrays will 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

( ) Seq.zip. , , ( ):

// 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
+8

, list1 list2. , ( , ).

+3
let pmap2 (list1:_ list) (list2:_ list)
+1
source

Source: https://habr.com/ru/post/1788810/


All Articles