-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMalList.HC
151 lines (134 loc) · 2.67 KB
/
MalList.HC
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
151
// Functions for working with list Malvals
#ifndef MALLIST_HC
#define MALLIST_HC
#include "Util.HC"
#include "PArray.HC"
#include "Types.HC"
BOOL MalIsList(Malval *m)
{
NullCheck(m, "malval");
return m->_tag == LIST;
}
// Doesn't make copy of input list
Malval *MalListMk(List *l)
{
NullCheck(l, "list");
Malval *m = _MalMk;
m->_tag = LIST;
m->_list = l;
m->_metadata = MalNilMk;
return m;
}
// Doesn't make copy of input list
Malval *MalListFromPArray(PArray *pa)
{
NullCheck(pa, "array");
Malval *m = _MalMk;
m->_tag = LIST;
m->_list = ListFromPArray(pa);
m->_metadata = MalNilMk;
return m;
}
Malval *MalListNil()
{
return MalListMk(ListNil);
}
INT MalListSize(Malval *l)
{
if (MalIsList(l)) {
return ListSize(l->_list);
}
else {
throws("MalListSize expects list argument");
return 0;
}
}
Malval *MalListNth(Malval *l, INT i)
{
if (MalIsList(l)) {
return ListNth(l->_list, i);
}
else {
return throws("MalListGet expects list argument");
}
}
Malval *MalListHead(Malval *l)
{
if (MalIsList(l)) {
if (MalListSize(l)) {
return ListHead(l->_list);
}
else return MalNilMk;
}
else {
return throws("MalListHead expects list argument");
}
}
Malval *MalListTail(Malval *l)
{
if (MalIsList(l)) {
if (MalListSize(l)) {
return MalListMk(ListTail(l->_list));
}
else {
return MalListNil;
}
}
else {
return throws("MalListTail expects list argument");
}
}
Malval *MalListCons(Malval *x, Malval *l)
{
if (MalIsList(l)) {
return MalListMk(ListCons(x, l->_list));
}
else {
return throws("MalListCons expects second argument to be a list");
}
}
Malval *MalListPush(Malval *l, Malval *x)
{
if (MalIsList(l)) {
return MalListMk(ListAppend(l->_list, x));
}
else {
return throws("MalListCons expects second argument to be a list");
}
}
Malval *MalListConcat(Malval *l1, Malval *l2)
{
if (MalIsList(l1) && MalIsList(l2)) {
return MalListMk(ListConcat(l1->_list, l2->_list));
}
else {
return throws("MalListConcat expects list arguments");
}
}
extern BOOL MalIsVector(Malval*);
Malval *MalListOfVector(Malval *vec)
{
NullCheck(vec, "vector");
if (!MalIsVector(vec)) {
return throws("MalListOfVector expects vector argument");
}
return MalListFromPArray(PArrayCopy(vec->_array));
}
Malval *MalListMap(Malval *l, CHAR* (*f) (CHAR*))
{
if (MalIsList(l)) {
return MalListMk(ListMap(l->_list, f));
}
else {
return throws("MalListMap expects first argument to be a list");
}
}
List *MalListOfList(Malval *v)
{
NullCheck(v, "malval");
if (v->_tag != LIST) {
throws("MalListOfList: expected number");
}
return v->_list;
}
#endif