-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhomework1.txt
150 lines (92 loc) · 3.27 KB
/
homework1.txt
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
Principles of Programming Languages course @ Politecnico di Milano
HOMEWORK 1
warning: not mandatory exercises, you won't get any additional points resolving them.
for questions write to [email protected]
exercise 1
define the "gcd" function.
gcd takes two numbers as input and computes the great commom divisor between the two.
examples:
(gcd 8 28) -> 4
(gcd 11 98) -> 1
---
exercise 2
define the "nested-length" function.
nested-length takes a list as argument and returns the number of element inside the list and all the nested lists.
examples:
(nested-length '(1 2 3)) -> 3
(nested-length '(1 2 (2 (5 4)) 5 6)) -> 7
---
exercise 3
define the "print-matrix" function.
print-matrix takes a matrix as argument (a matrix is a vector of vectors, e.g. #(#(1 2) #(3 4)) is a 2x2 matrix)
and prints out the content of the matrix as in following examples.
examples:
> (print-matrix #(#(1 2) #(3 4)))
1 2
3 4
> (print-matrix #(1 2 3))
1
2
3
---
exercise 4
define the "vector*!" function.
vector*! takes three vectors (v1 v2 v3) as arguments.
this function will consider v2 a Nx1 matrix, v3 a 1xM matrix; v1 must be a mutable vector of size N.
this function computes the multiplication between v2 and v3 and saves the resulting NxM matrix in v1.
example:
> (define v1 (make-vector 2))
> (vector*! v1 #(3 2) #(4 5))
> (print-matrix v1)
12 15
8 10
> (vector*! v1 #(5 2) #(1 1 0))
> (print-matrix v1)
5 5 0
2 2 0
---
exercise 5
define the "zip" function.
zip takes two lists as arguments. it returns a list of lists, where the i-th list contains the i-th element from each input list.
the returned list is truncated in length to the length of the shortest argument list.
examples:
(zip '(1 2 3) '(4 5 6)) -> '((1 4) (2 5) (3 6))
(zip '(a 4 d) '(2 c)) -> '((a 2) (4 c))
---
exercise 6
define the "split" function.
split takes two arguments: a list and a splitter value.
it returns a list of lists in which each list is the sublist of the input list between two occurences of the splitter value.
examples:
(split '(a b c x d x e x f) 'x) -> '((a b c) (d) (e) (f))
(split '(0 1 2 0 2 0 3 0) 0) -> '((1 2) (2) (3))
---
exercise 7
define the "takewhile" function.
takewhile takes two arguments: a predicate and a list. it returns the elements of the list until the predicate is violated.
examples:
(takewhile odd? '(1 5 7 8 1 4 6 9)) -> '(1 5 7)
(takewhile positive? '(5 11 4 2 -2 5 6) -> '(5 11 4 2)
---
exercise 8
define the "flatmap" function.
flatmap is similar to map but it works also for nested lists. it returns a flatten list.
examples:
(flatmap square '(((1 2 3) 4) 0 (6 7) 8)) -> '(1 4 9 16 0 36 49 64)
(flatmap (lambda(x) (+ x 1)) '(5 (11) ((12 23) 5))) -> '(6 12 13 24 6)
---
exercise 9
define the "on-sign" macro.
on-sign choose between three branches depending if the value considered is greater than zero, equals to zero, or less than zero.
this macro has the following syntax:
(on-sign <my expr> pos: <block of code> zero: <block of code> neg: <block of code>)
examples:
(on-sign 2
pos: 'a
zero: 'b
neg: 'c) -> 'a
> (on-sign (- 5 6)
pos: (begin (display "positive") (newline))
zero: (begin (display "zero") (newline))
neg: (begin (display "negative") (newline)))
negative