-
Notifications
You must be signed in to change notification settings - Fork 5
/
live4.fsx
136 lines (95 loc) · 2.51 KB
/
live4.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
(*
ITT8060 -- Advanced Programming 2013
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 5: making types and values
James Chapman and Juhan Ernits
Material based on chapter 5 of RWFP
*)
// int * int
// 1 , 2
let pair = 1 , 2
let divRem (a,b) = a / b, a % b
(*
int DivRem (int a, int b, out int rem) {
rem = a % b;
return a / b;
}
int rem;
int res = DivRem(10,3,out rem);
*)
(*
int parsed;
bool success = Int32.TryParse("41", out parsed);
*)
open System
let tp = Int32.TryParse("41")
let tp = 1 , "hello"
let msgAt1 = 50 , 100, "Hello world"
let msgAt2 = (50, 100), "Hello world"
let printMessage (x, y) message =
printfn "[%d %d] %s" x y message
let x, y, _ = msgAt1
printMessage (x,y) "test1"
let coords, _ = msgAt2
printMessage coords "test2"
printMessage (fst msgAt2) "test3"
open System
type Schedule =
| Never
| Once of DateTime
| Repeatedly of DateTime * TimeSpan
Never
Once
Repeatedly
let tomorrow = DateTime.Now.AddDays(1.0)
let noon = new DateTime(2008,8,1,12,0,0)
let daySpan = new TimeSpan(24,0,0)
let schedule1 = Never
let schedule2 = Once tomorrow
let schedule3 = Repeatedly(noon, daySpan)
let getNextOccurence schedule =
match schedule with
| Never -> DateTime.MaxValue
| Once eventDate ->
if eventDate > DateTime.Now then eventDate
else DateTime.MaxValue
| Repeatedly (startDate,interval) ->
let secondsFromFirst = (DateTime.Now - startDate).TotalSeconds
let q = secondsFromFirst / interval.TotalSeconds
let q = max q 0.0
startDate.AddSeconds (interval.TotalSeconds * (Math.Floor q + 1.0))
let next1 = getNextOccurence Never
let next2 = getNextOccurence schedule2
let next3 = getNextOccurence schedule3
type Tree =
| Leaf of int
| Node of Tree * Tree
let tree1 = Leaf 1
let tree2 = Node (Leaf 1, Leaf 2)
let tree3 = Node (Leaf 1, Node (Leaf 2, Leaf 3))
let tree4 = Node (tree3, tree2)
type IntOption =
| SomeInt of int
| NoneInt
open System
let readInput () =
let s = Console.ReadLine ()
match Int32.TryParse s with
| true , parsed -> SomeInt parsed
| _ -> NoneInt
readInput()
readInput()
type MyOption<'T> =
| MySome of 'T
| MyNone
type OptionallyLabelled<'T1,'T2> =
| LabeledTuple of string * 'T1 * string * 'T2
| UnlabeledTuple of 'T1 * 'T2
LabeledTuple ("Seven", 7, "Pi", 3.14)
UnlabeledTuple (7,3.14)
let num = 123
let tup = 123, "hello world"
let input =
printfn "Calculating..." ; if num = 0 then None else Some (num.ToString())