-
Notifications
You must be signed in to change notification settings - Fork 6
/
lecture6.fsx
121 lines (85 loc) · 2.61 KB
/
lecture6.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(*
ITT8060 -- Advanced Programming 2015
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 6 Higher order functions
Reading: Chapter 6 in RWFP
Additional reading: http://fsharpforfunandprofit.com/posts/elevated-world-2/
Juhan Ernits
*)
open System
type Schedule =
| Never
| Once of DateTime
| Repeatedly of DateTime * TimeSpan
// this is broken, because the type of d is not known yet
(fun d -> d.AddDays(7.0)) (DateTime(2015,10,14))
// fix
(DateTime(2015,10,14)) |> (fun d -> d.AddDays(7.0))
// fix 2
(fun (d:DateTime) -> d.AddDays(7.0)) (DateTime(2015,10,14))
((fun (d:DateTime) -> d.AddDays(7.0)) : DateTime -> DateTime) (DateTime(2015,10,14))
((fun d -> d.AddDays(7.0)) : DateTime -> DateTime) (DateTime(2015,10,14))
(DateTime(2015,10,14)).AddDays(7.0)
let mapSched (f: DateTime -> DateTime) (sched: Schedule) : Schedule =
match sched with
| Never -> Never
| Once dt -> Once (f dt)
| Repeatedly (dt, ts) -> Repeatedly (f dt, ts)
mapSched (fun d -> d.AddDays(7.0)) (Once (DateTime(2015,10,15)))
let scheds = [Never; Once (DateTime(2015,10,15))]
List.map (mapSched (fun d -> d.AddDays(7.0))) scheds
let readInput() =
let input = Console.ReadLine()
match Int32.TryParse input with
| true, i -> Some i
| false, _-> None
let readAndAdd1() =
match readInput() with
| None -> None
| Some n ->
readInput() |> Option.map ((+) n)
let readAndAdd2() =
match readInput() with
| None -> None
| Some n ->
match readInput() with
| None -> None
| Some m -> Some (n + m)
readAndAdd1()
let map f opt =
match opt with
| None -> None
| Some x -> Some (f x)
let bind f opt =
match opt with
| None -> None
| Some x -> f x
let readAndAdd3() = readInput() |> bind (fun n -> readInput() |> Option.map ((+) n))
readAndAdd3()
//1 / 0
let parseInt str =
match str with
| "-1" -> Some -1
| "0" -> Some 0
| "1" -> Some 1
| "2" -> Some 2
| _ -> None
type OrderQty = OrderQty of int
let toOrderQty qty =
if qty >= 1 then
Some (OrderQty qty)
else
None
//Option.bind is a built in implementation of the above bind
let parseOrderQty str =
parseInt str |> Option.bind toOrderQty
// is equivalent to
let parseOrderQty2 str =
Option.bind toOrderQty (parseInt str)
parseOrderQty "2"
parseOrderQty "-1"
//let parseOrderQty3 str =
// (str |> parseInt) >>= toOrderQty
//Type inference example done on the board. As in section 6.2.2 in RWFP.