-
Notifications
You must be signed in to change notification settings - Fork 5
/
live2.fsx
173 lines (124 loc) · 3.63 KB
/
live2.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
(*
ITT8060 -- Advanced Programming 2013
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 2: values, functions, tuples and lists
James Chapman and Juhan Ernits
Material based on chapter 3 of RWFP
*)
let number = 24
printfn "%d" number
let message = "Answer: " + number.ToString ()
printfn "%s" message
let number = 24 in
(
printfn "%d" number;
let message = "Answer: " + number.ToString () in printfn "%s" message
)
let multiply (num1 : float) (num2 : int) = num1 * float num2
let multiply = fun num1 -> fun num2 -> num1 * num2
let printSquares message num1 num2 =
let printSquareUtility num =
let square = num * num
printfn "%s %d: %d" message num square
printSquareUtility num1
printSquareUtility num2
printSquares "Square of:" 14 27
let n1 = 22
n1 <- 23
let mutable n2 = 22
n2 <- 23
n2
// tuples
let tp = "Hello world", 42
let prague = "Prague", 1188126
let seattle = "Seattle", 594210
let printCity cityInfo =
printfn "Population of %s is %d." (fst cityInfo) (snd cityInfo)
printCity prague
printCity seattle
let newyork = "New York", 718000
printCity newyork
//let withItem2 newItem2 tuple = fst tuple, newItem2
let withItem2 newItem2 tuple =
let originalItem1, originalItem2 = tuple
originalItem1, newItem2
let withItem2 newItem2 tuple =
let originalItem1, _ = tuple
originalItem1, newItem2
let withItem2 newItem2 (originalItem1, _) = originalItem1, newItem2
let withItem2 newItem2 tuple =
match tuple with
| originalItem1, _ -> originalItem1, newItem2
let setPopulation tuple newPopulation =
match tuple with
| "New York", _ -> "New York", newPopulation + 100
| cityName, _ -> cityName, newPopulation
let prague = "Prague", 123
let newyork = "New York", 123
setPopulation prague 10
setPopulation newyork 10
let rec factorial n =
if n <= 1 then 1 else n * factorial (n-1)
factorial 5
let ls1 = []
let ls2 = 1 :: ls1
let ls3 = [6; 2; 7; 3]
let ls4 = 1 :: ls3
let ls5 = 6 :: (2 :: (7 :: (3 :: []))) // same as ls3
let ls5 = 6 :: 2 :: 7 :: 3 :: [] // due to right assoc. of ::
// array interlude
let ar1 = [| 6; 2; 7; 3 |]
ar1.[0]
// back to lists
let ls6 = [ 1 .. 10 ]
let ls6a = [ 0 .. 2 .. 10 ] // changed step to 2
let ls7 = ls6 @ ls5
let startsWith list =
match list with
| [] -> printfn "Empty list"
| head::_ -> printfn "Starts with %d" head
startsWith [4;5;6]
startsWith []
let squareFirst list =
match list with
| head::_ -> head * head
squareFirst [4;5;6]
squareFirst []
let rec sumList (list : int list) : int =
match list with
| [] -> 0
| head::tail -> head + sumList tail
let list = [ 1 .. 5 ]
sumList list
let rec zip (lista : 'a list) (listb : 'b list) : ('a * 'b) list =
match (lista, listb) with
| [],_ -> []
| _,[] -> []
| (heada::taila),(headb::tailb) -> (heada,headb) :: zip taila tailb
let plist = zip [ 1 .. 10 ] [ 11 .. 21 ]
let rec unzip (plist : ('a * 'b) list) : ('a list) * ('b list) =
match plist with
| [] -> [],[]
| (heada,headb)::ptail ->
let taila,tailb = unzip ptail
(heada::taila),(headb::tailb)
unzip plist
let rec sumList (list : int list) : int =
match list with
| [] -> 0
| head::tail -> head + sumList tail
let rec prodList (list : int list) : int =
match list with
| [] -> 1
| head::tail -> head * prodList tail
// fold
let rec aggregateList (op : int -> int -> int) (init : int) (list : int list) : int =
match list with
| [] -> init
| head::tail -> op head (aggregateList op init tail)
let sumList list = aggregateList (+) 0 list
sumList [1..5]
let prodList list = aggregateList (*) 1 list
prodList [1..5]