-
Notifications
You must be signed in to change notification settings - Fork 5
/
live7.fsx
81 lines (62 loc) · 1.81 KB
/
live7.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(*
ITT8060 -- Advanced Programming 2013
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 7: options, lists, higher order functions, generics
Based on chapter 6 of RWFP
James Chapman and Juhan Ernits
*)
open System
type Schedule =
| Never
| Once of DateTime
| Repeatedly of DateTime * TimeSpan * option<int>
let schedules = [ Never
Once (DateTime (2008,1,1))
Repeatedly (DateTime (2008,1,2), TimeSpan (24*7,0,0), Some 2)]
let mapSchedule (rescheduleFunc: DateTime -> DateTime) schedule =
match schedule with
| Never -> Never
| Once eventDate -> Once (rescheduleFunc eventDate)
| Repeatedly (startDate, interval, times) ->
Repeatedly (rescheduleFunc startDate, interval, times)
for s in schedules do
let newSchedule = mapSchedule (fun d -> d.AddDays (7.0)) s
printfn "%A" newSchedule
let newSchedules =
schedules |> List.map (mapSchedule (fun d -> d.AddDays (7.0)))
let add5 (on : option<int>) : option<int> =
match on with
| Some n -> Some (n + 5)
| None -> None
add5 (Some 5)
add5 None
open System
let readInput() =
let s = Console.ReadLine()
match Int32.TryParse s with
| true , i -> Some i
| false , _ -> None
let readAndAdd1() =
match readInput () with
| None -> None
| Some m ->
match readInput () with
| None -> None
| Some n -> Some (m + n)
let readAndAdd2() =
match readInput () with
| None -> None
| Some m -> readInput() |> Option.map (fun n -> m + n)
let readAndAdd3() =
readInput() |> Option.bind (fun m ->
readInput() |> Option.map (fun n -> m + n))
let map f input =
match input with
| None -> None
| Some v -> Some (f v)
let bind f input =
match input with
| None -> None
| Some v -> f v