In F# if-then-else is a normal expression, so instead of "a = b < c ? b : c" you can use "let a = if b < c then b else c". Sometimes you need to put extra parenthesis around the if-then-else expression.


By on 11/7/2007 12:15 PM ()

It seams like Option values fit will into the ternary discussion. Here is some code I just wrote, but I'm not sure which one is more readable or performant.

let headers = if headerArray.IsNone then new ResizeArray<Header>() else new ResizeArray<Header>(headerArray.Value)

let headers2 = match headerArray with | None -> new ResizeArray<Header>() | Some(v) -> new ResizeArray<Header>(v)

The function argument is headerArray:Header[] option. What do you think?


By on 6/24/2008 12:02 PM ()

It seams like Option values fit will into the ternary discussion.

Wow! I just discovered that "match" is an expression! That makes the following code valid (though nonsensical in this example case):

let fn x y = x + match y with 1->0 | 2->10 | 3->100 | _ -> y*1000;;
By on 6/25/2008 11:30 PM ()


Everything is an expression in F# (well, except global declarations). "fun .." and "function .." are also expressions, returning functions. for and while are expressions too, returning a unit type.

let f x y = <expression>

So, everything you can have on a right-side of a let is an expression. Everywhere the parser expects an expression, you can write a if, a match, a local definition (even local functions), etc. Yes, that's quite flexible. :)

By on 6/26/2008 3:40 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper