-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlecture5.fsx
140 lines (89 loc) · 2.91 KB
/
lecture5.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
(*
ITT8060 -- Advanced Programming 2015
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 5: Option, functional values, generics
Reading: Chapter 5 in RWFP
* Section 3.11 introduces option
* The other concepts are thoroughly explained in Chapters 4 and 5.
Juhan Ernits
*)
open System
Int32.TryParse "1"
Int32.TryParse "noise"
type IntOption = | IntSome of int
| IntNone
let readInput1() =
let input = Console.ReadLine()
match Int32.TryParse input with
| true, i -> IntSome i
| _ -> IntNone
readInput1()
// Built-in type
//type Option<'a> = | Some of 'a
// | None
// OCaml syntax for the built-in option.
//type 'a option = | Some of 'a
// | None
let readInput2() =
let input = Console.ReadLine()
match Int32.TryParse input with
| true, i -> Some i
| _ -> None
let multiplyWith (scalar: int) (parsedData : int option) =
match parsedData with
| Some x -> scalar * x
| None -> 0
multiplyWith 2 (Some 5)
multiplyWith 3 None
multiplyWith 3 (readInput2())
let readValue (opt : 'a option) : 'a =
match opt with
| Some n -> n
| None -> failwith "No value"
let readValue2 opt =
match opt with
| Some n -> n
| None -> failwith "No value"
readValue (Some 2)
// lecture5.fsx(69,1): error FS0030: Value restriction. The value 'it' has been inferred to have generic type
// val it : '_a
// Either define 'it' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.
// need to fix it by a concrete type parameter
readValue (None : 'a option)
// e.g.
readValue (None : int option)
let nums = [4 ; 9; 1; 8; 6]
let evenTest n = (n % 2 = 0)
let evens = List.filter evenTest nums
let square1 (a : int) : int = a * a
let square2 : int -> int = fun a -> a * a
let square2_2 = fun a -> a * a
let add : int -> int -> int = fun a b -> a + b
let twice input f = f (f input)
twice 2 square1
// not functional style
let add2 = fun (a, b) -> a + b
// cannot do that add2 (2,_)
let add3 = fun a b -> a + b
let add4 = fun a -> fun b -> a + b
let addTen = add 10
List.map addTen [ 1.. 10 ]
List.map (fun a -> a 1) (List.map add [1 .. 10])
let (|>) x f = f x
List.map add [1 .. 10] |> List.map (fun a -> a 1)
List.head( List.rev [1 .. 5])
[1;2;3] @ [4;5]
let oldPrague = "Prague" , 123
// first attempt
let name, pop = oldPrague
let newPrague = name, pop + 1
//second attempt
let newPrague2 = fst oldPrague, snd oldPrague + 1
//third attempt
let mapSecond f (a, b) = a, f b
let newPrague3 = oldPrague |> mapSecond ((+) 1)
let bimap f g (a, b) = f a, g b
let mapFirst f p = bimap f id p
let id a = a