Match an Array in F#
The closest thing you'll get without converting to a list is:
match argv with
| arr when argv.Length > 0 ->
let first = arr.[0]
printfn "%s" first
| _ -> printfn "none"
If you convert argv
to a list using Array.toList
, you can then pattern match on it as a list using the cons operator, ::
:
match argv |> Array.toList with
| x::[] -> printfn "%s" x
| x::xs -> printfn "%s, plus %i more" x (xs |> Seq.length)
| _ -> printfn "nothing"
You can use truncate
:
match args |> Array.truncate 1 with
| [| x |] -> x
| _ -> "No arguments"