-
Notifications
You must be signed in to change notification settings - Fork 6
/
lecture7.fsx
222 lines (120 loc) · 4.31 KB
/
lecture7.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(*
ITT8060 -- Advanced Programming 2015
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 7: Interaction with .Net libraries, Charting, behaviour centric programming
Juhan Ernits
*)
List.map
Option.map
List.fold
Option.fold
List.filter
Option.filter
List.fold (+) 0 [1;2;3]
Option.fold (+) 1 (Some 1)
Option.fold (+) 1 None
(+)
List.fold (fun x y -> x+y) 0 [1;2;3]
Option.bind
List.collect
open System.IO
let directories =
[ "c:\Windows";
"c:\Program files" ]
directories |> List.collect (fun d ->
d |> Directory.GetFiles
|> List.ofSeq
|> List.map Path.GetFileName )
directories |> List.collect (fun d ->
d |> Directory.GetFiles
|> List.ofSeq )
List.ofSeq [|1;2;3|]
List.ofArray [|1;2;3|]
#r @"..\packages\FSharp.Charting.0.90.12\lib\net40\FSharp.Charting.dll"
open FSharp.Charting
open FSharp.Charting.ChartTypes
Chart.Line([for x in 0 .. 10 -> x, x * x]).ShowChart()
//alternative way to load the library
#load @"..\packages\FSharp.Charting.0.90.12\FSharp.Charting.fsx"
let rnd = System.Random()
let rand() = rnd.NextDouble()
let randomPoints = [for i in 0 .. 1000 -> 10.0 * rand() , 10.0 * rand()]
Chart.Point(randomPoints)
let randomTrend1 = [for i in 0.0 .. 0.1 .. 10.0 -> i, sin i + rand()]
let randomTrend2 = [for i in 0.0 .. 0.1 .. 10.0 -> i, sin i + rand()]
Chart.Combine [Chart.Line (randomTrend1, Title = "Awesome chart") ; Chart.Point randomTrend2]
// Records and behaviour centric programming
type Rect =
{
Left : float32
Top : float32
Width : float32
Height : float32
}
let rc = {Left = 10.0f; Top = 10.0f ; Width = 100.0f ; Height = 200.0f }
rc.Left + rc.Width
let rc2awk = { Left = rc.Left + 100.0f; Top = rc.Top;
Width = rc.Width; Height = rc.Height }
let rc2 = { rc with Left = rc.Left + 100.0f}
type Client =
{ Name : string; Income : int ; YearsInJob : int
UsesCreditCard : bool; CriminalRecord : bool }
let john = {Name = "John Doe"; Income = 40000 ; YearsInJob = 1 ;
UsesCreditCard = true ; CriminalRecord = false }
let tests =
[ (fun cl -> cl.CriminalRecord = true) ;
(fun cl -> cl.Income < 30000) ;
(fun cl -> cl.UsesCreditCard = false) ;
(fun cl -> cl.YearsInJob < 2)
]
let testClient client =
let issues = tests |> List.filter (fun f -> f client)
let suitable = issues.Length<=1
printfn "Client: %s\n Offer a loan %s (issues= %d)" client.Name
(if suitable then "Yes" else "No") issues.Length
testClient john
// Lecture 8
type Client =
{ Name : string; Income : int ; YearsInJob : int
UsesCreditCard : bool; CriminalRecord : bool }
type QueryInfo =
{ Title : string
Check : Client -> bool
Positive : Decision
Negative : Decision }
and Decision =
| Result of string
| Query of QueryInfo
let rec tree =
Query {Title = "More than €40k"
Check = (fun cl -> cl.Income > 40000)
Positive = moreThan40
Negative = lessThan40}
and moreThan40 =
Query {Title = "Has criminal record"
Check = (fun cl -> cl.CriminalRecord)
Positive = Result "NO"
Negative = Result "YES"}
and lessThan40 =
Query {Title = "Years in job"
Check = (fun cl -> cl.YearsInJob > 1)
Positive = Result "YES"
Negative = usesCreditCard}
and usesCreditCard =
Query {Title = "Uses credit card"
Check = (fun cl -> cl.UsesCreditCard)
Positive = Result "YES"
Negative = Result "NO"}
let rec testClientTree client tree =
match tree with
| Result msg -> printfn " OFFER A LOAN: %s" msg
| Query qinfo -> let result, case =
if qinfo.Check(client) then "yes", qinfo.Positive
else "no", qinfo.Negative
printfn " - %s ? %s" qinfo.Title result
testClientTree client case
let john = {Name = "John Doe"; Income = 40000 ; YearsInJob = 1 ;
UsesCreditCard = true ; CriminalRecord = false }
testClientTree john tree