Reputation: 4302
I have written this F# code:
let tuples = [|("A",1,0);("A",2,3);("A",3,6)|]
let tupleSubset =
tuples
|> Seq.map(fun values ->
values.[0],
values.[2])
printfn "%A" tupleSubset
I am getting: The operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point. Consider adding further type constraints
Can anyone tell me what I am doing wrong?
Upvotes: 4
Views: 3076
Reputation: 9825
The explanation of Shredderoy is correct, accessing an element by its index is reserved to arrays, which are contiguous blocks of memory.
For tuple, if you want to access the values of a tuple in F# the general method is to pattern match
let tuple = a,b,c
let (x,y,z) = tuple
In the case of pair, the function fst
and snd
also give a similar access
PS : In you initial array, you dont need to use parenthesis, as the compiler recognizes the ,
as a tuple
Upvotes: 0
Reputation: 2920
A simple way around the error you are getting would be the following:
let tuples = [|("A",1,0); ("A",2,3); ("A",3,6)|]
let tupleSubset = tuples |> Seq.map (fun (a, b, c) -> a, b)
printfn "%A" tupleSubset
As to why you are getting the error: note that the kind of index dereferencing you are attempting with values.[0]
and values.[1]
works for arrays, dictionaries, etc., but not for tuples, whereas each values
has the type string * int * int
.
Since you don't need the third element of the tuple, you can even choose not to bind it to a symbol, by writing the second line as follows:
let tupleSubset = tuples |> Seq.map (fun (a, b, _) -> a, b)
Upvotes: 7