开发者

shorter way to write match

I have a function that returns a tuple:

let rec pack l = 
            let combine = List.fold packFunction (' ',[], []) l
            match combine with 
                            | (_,b,a) -> b::a |> List.rev |> List.tail

is there a way to extract parts of the tuple without using a match statement?

ie, id like to get b and a out of (_,b,a) without using a match s开发者_StackOverflowtatement


Something like this?

let rec pack l = 
    let _,b,a = List.fold packFunction (' ',[], []) l
    b::a |> List.rev |> List.tail

You can always extract directly from tuples:

let a,b = (1,1)

or

let a,b = functionWhichReturnsTuple


Pattern matching can be used in many places other than just matches. In this case, it doesn't appear that you need combine at all (nor does it appear that pack needs to be recursive)...

let pack l = 
    let _, b, a = List.fold packFunction (' ', [], []) l
    b::a |> List.rev |> List.tail


Yet another way to put it (patter matching via lambda argument):

let rec pack l =
    List.fold packFunction (' ',[], []) l
    |> fun (_,b,a) -> b::a |> List.rev |> List.tail           
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜