Accessing a specific member in a F# tuple

前端 未结 4 651
南旧
南旧 2021-02-03 18:20

In F# code I have a tuple:

let myWife=(\"Tijana\",32)

I want to access each member of the tuple separately. For instance this what I want to ac

4条回答
  •  没有蜡笔的小新
    2021-02-03 18:51

    You can also write an unpack function for a certain length:

    let unpack4 tup4 ind =
        match ind, tup4 with
        | 0, (a,_,_,_) -> a
        | 1, (_,b,_,_) -> b
        | 2, (_,_,c,_) -> c
        | 3, (_,_,_,d) -> d
        | _, _ -> failwith (sprintf "Trying to access item %i of tuple with 4 entries." ind) 
    

    or

    let unpack4 tup4 ind =
        let (a, b, c, d) = tup4
        match ind with
        | 0 -> a
        | 1 -> b
        | 2 -> c
        | 3 -> d
        | _ -> failwith (sprintf "Trying to access item %i of tuple with 4 entries." ind) 
    

提交回复
热议问题