-
Notifications
You must be signed in to change notification settings - Fork 5
/
coursework7.fsx
106 lines (74 loc) · 2.68 KB
/
coursework7.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
(*
ITT8060 -- Advanced Programming 2013
Department of Computer Science
Tallinn University of Technology
------------------------------------
Coursework :7 F# and unit tests
------------------------------------
Name:
Student ID:
------------------------------------
Answer the questions below. You answers to questions should be
correct F# code written after the question. This file is an F#
script file, it should be possible to load the whole file at
once. If you can't then you have introduced a syntax error
somewhere.
This coursework will be graded. When submitting the coursework,
call the file you submit "Lastname_Firstname.fs" and
"Lastname_Firstname.dll" with your first
and last name appropriately, attach it to an e-mail with
subject line "[ITT8060] Coursework 7", and send it to
itt8060@cs.ttu.ee.
The coursework is due on November 8.
*)
// Place the following functions into an appropriate module, write unit tests
// using Xunit and run the tests to ensure they all pass before submitting
// the coursework.
// 1) Rebrackeding function:
let rebracket ((x, y), z) = (x, (y, z))
// 2) GetNextOccurrence
// getNextOccurence function.
open System
type Schedule =
| Never
| Once of DateTime
| Repeatedly of DateTime * TimeSpan
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))
// 3) Test the function that builds a tree from a list.
type Tree =
| Node of (Tree * Tree)
| Leaf of int
let rec makeTree list =
match list with
| a :: [] -> Leaf a
| a :: tail -> Node (Leaf a, makeTree tail)
| [] -> failwith "Can't make a tree from empty list"
// 4) Test the function that flattens a tree to a list.
let rec flatten tree =
match tree with
| Leaf a -> [a]
| Node (left, right) -> flatten left @ flatten right
// 5) Test the combination of the two previous functions.
// 6) Test the summing and converting operations on the given data structure.
type STree =
| SNode of (STree * int * STree)
| SLeaf of int
// Function to sum elements in the tree
let rec sum tree =
match tree with
| Leaf a -> a
| Node (left, right) -> sum left + sum right
let rec convert tree =
match tree with
| Leaf a -> SLeaf a
| Node (left, right) -> SNode (convert left, sum left + sum right, convert right)