Marcom
Marcom

Reputation: 4741

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 statement

Upvotes: 3

Views: 164

Answers (3)

Ed'ka
Ed'ka

Reputation: 7140

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           

Upvotes: 1

ildjarn
ildjarn

Reputation: 62975

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

Upvotes: 2

Lasse Espeholt
Lasse Espeholt

Reputation: 17782

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

Upvotes: 6

Related Questions