This project is read-only.
Using options for data

Category:
Lists, Tuples and Options
Description: Here we return an option from a function indicating the opening hours of a shop, if any.
Code:
let OptionsSample3() =

let openingHours day =
match day with
| DayOfWeek.Monday
| DayOfWeek.Tuesday
| DayOfWeek.Thursday
| DayOfWeek.Friday -> Some(9,17)
| DayOfWeek.Wednesday -> Some(9,19) // extended hours on Wednesday
| _ -> None

let today = DateTime.Now.DayOfWeek

match openingHours today with
| None -> printfn "The shop's not open today"
| Some(s,f) -> printfn "The shop's open today from %02d:00-%d:00" s f

//--------------------------------------------------------

Execution Result:
The shop's open today from 09:00-17:00

Last edited Sep 14, 2011 at 3:55 AM by ttliu2000, version 1

Comments

No comments yet.