-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.fay
85 lines (70 loc) · 1.51 KB
/
matrix.fay
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
def make_matrix(a,b,c,d) {
match fn {
0 => match fn {
0=> a,
1=> b,
},
1 => match fn {
0=> c,
1=> d,
},
:n=>2,
:m=>2,
}
}
def matrix_mul(A,B) {
match A(:n)==B(:m){
false => {return :err;},
true => {}
};
make_entry = fn(n,m) -> {
fn(i,agg) -> {
match i==A(:n){
true =>{return agg;},
false =>{}
};
c = A(i)(m)*B(n)(i);
self(i-1,c+agg)
}(A(:n),0)
};
make_entry(1,1);
make_entry(0,0);
_make_row = fn(i,m,row) {
match i==A(:m){
true =>{return row;},
false =>{}
};
row = fn(x) {
match x==i {
true => make_entry(m,i),
false=> row(x)
}
};
self(i+1,m,row)
};
empty_row= match fn {_ => :err_matrix};
make_row = fn(m) -> {
_make_row(0,m,empty_row)
};
fn(x) -> {
match x {
:m => A(:m),
:n => B(:n),
_ => make_row(x)
}
}
}
#catches bugs related to closures fairly well
def main(system) {
a = 1;
b = 1;
c = 1;
d = 1;
matrix = make_matrix(a,b,c,d);
matrix(1)(1) |> system(:println)();
matrix = matrix_mul(matrix,matrix) |> system(:println)();
matrix = matrix_mul(matrix,matrix);
matrix |> system(:println)();
x = matrix(1)(1);
system(:println)( x )
}