I'm guessing you forgot "#light" at the start of your fsi session, because everything else looks okay. Alternatively you can add ;; at the end of each line.

"Some" is part of the option type, which is a discriminating union defined in the F# base libraries. It's definition is:

type option<'a> =

| Some of 'a

| None

It's very useful when you have a value that's, well, optional. A much better choice than null.

By on 12/18/2008 3:11 AM ()

Indeed!!!... it was the #light that I overlooked.

I'm not sure if I understood the Some though. Let me look around, think about this and ask a more specific question. Thanks for the help, I am going through the tutorials on your website and they seem more explanatory to someone like me.

Thanks.

By on 12/18/2008 4:03 AM ()

Some|None seems to be a mechanism for getting around the non-nilability/non-dynamicness of variables. For example, if your function divides two numbers like this:

1
let div x y = x / y

but you'd like to return a "fail" value if the denominator is 0, in other languages you could write:

1
2
3
4
5
6
let div x y =
    if y = 0 then
        return nil
    else
        return x / y
    end

you'd then be in charge of checking for an error (if you cared) at the places where you use the function. however i believe in F# the nil (actually called 'null') only applies to .NET stuff, or something of the sort. you can get around this by the "discriminated union" Some 'blah | None

1
2
3
4
5
6
let div x y =
    if y = 0 then
        return None
    else
        return (Some (x / y))
    end

(pretending 'return' exists in F#.) this tomfoolery is necessary because the function has to return the same type at all exits, and both None and Some (x / y) are in the same "discriminated union" by definition. then wherever you use the function you can un-Some (e.g. by pattern matching) to get the value out. something like

1
let Some value = div 1 2

or

1
2
3
match div 1 2 with
| None -> printfn "error!"
| Some value -> printfn value

in short, the language isn't dynamic, so it has to resort to complicated FP/OOP/type stuff ^_^

By on 12/19/2008 1:48 PM ()

Said another way, the Some type is a way to have deterministic handling of "there is no value" values, thus avoiding a common and sometimes hard to resolve run-time error.

The issue with nil as implemented in most languages is that one never knows if a function returns a nil value, and one can never be certain that a nil return is properly handled.

The Some type also allows the compiler to statically check to see if code properly handles such "no value" cases. For example, if only the "Some" portion is handled, the compiler produces a warning.

1
2
3
4
5
6
7
#light
let div x y = if y=0 then None else Some(x/y)
let test =
    match (div 1 2) with
    | Some value -> printfn "%i" value

warning FS0025: Incomplete pattern matches on this expression. For example, the value 'None' will not be matched
By on 12/19/2008 3:16 PM ()

Thanks guys. That makes it quite clear.

By on 12/28/2008 10:54 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper