How do I use Some/None Options in this F# example?

Posted by Phobis on Stack Overflow See other posts from Stack Overflow or by Phobis
Published on 2010-05-13T18:54:55Z Indexed on 2010/05/13 19:14 UTC
Read the original article Hit count: 278

I am new to F# and I have this code:

if s.Contains("-") then
    let x,y =
      match s.Split [|'-'|] with
      | [|a;b|] -> int a, int b
      | _ -> 0,0

Notice that we validate that there is a '-' in the string before we split the string, so the match is really unnecessary. Can I rewrite this with Options?

I changed this code, it was originally this (but I was getting a warning):

if s.Contains("-") then
    let [|a;b|] = s.Split [|'-'|] 
    let x,y = int a, int b

NOTE: I am splitting a range of numbers (range is expressed in a string) and then creating the integer values that represent the range's minimum and maximum.

© Stack Overflow or respective owner

Related posts about F#

Related posts about patterrn-matching