-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcoursework4.fsx
78 lines (60 loc) · 2.31 KB
/
coursework4.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
(*
ITT8060 -- Advanced Programming 2015
Department of Computer Science
Tallinn University of Technology
------------------------------------
Coursework 4: Higher order functions, option, list
------------------------------------
Name:
Student ID:
------------------------------------
Answer the questions below. You answers to the 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.
Commit and push your solution to the repository as file
coursework4.fsx in directory coursework4.
The deadline for completing the above procedure is Friday,
October 23, 2015.
We will consider the submission to be the latest version of the
appropriate files in the appropriate directory before the deadline
of a particular coursework.
*)
// 1. Write a function by pattern matching
//
// flattenOption : option<option<'a>> -> option<'a>
//
// which squashes two layers of possible successes or failures into 1
// E.g. Some Some 1 -> Some 1
// 2. Can flattenOption by implemented using bind? If so, do it!
// 3. Write a function
//
// defeatist : list<option<'a>> -> option<list<'a>>
//
// that takes a list of possible successes or failures and returns
// a list of successes if everything succeeded or returns failure
// if 1 or more elements of the list was a failure. Again, pay
// close attention to the type.
// E.g. [Some 1 ; Some 2] -> Some [1; 2]
// 4. Write a function
//
// optimist : 'a -> list<option<'a>> -> list<'a>
//
// which collects a list of possible successes or failures into a
// list containing only the successes with all failures replaced
// by the first parameter of the function. Pay close attention to the type.
// E.g. optimist 0 [Some 1; None] -> [1; 0]
// 5. Write a function
//
// chars : list<string> -> list<char>
//
// This function should use List.collect (bind) and have the
// following behaviour:
// ["hello";"world"] -> ['h';'e';'l';'l';'o';'w';'o';'r';'l';'d']
// 6. Write a function
//
// iprint : list<int> -> string
//
// This function should use List.foldBack and have the following behaviour:
// [1 .. 5] |-> "1,2,3,4,5,"