"cons": { a -> [a[0]] + (a[1] as List) },
"concat": core.&do_concat,
"nth": core.&do_nth,
- "first": { a -> a[0].size() == 0 ? null : a[0][0] },
- "rest": { a -> a[0].drop(1) },
+ "first": { a -> a[0] == null || a[0].size() == 0 ? null : a[0][0] },
+ "rest": { a -> a[0] == null ? [] as List : a[0].drop(1) },
"empty?": { a -> a[0] == null || a[0].size() == 0 },
"count": { a -> a[0] == null ? 0 : a[0].size() },
"apply": core.&do_apply,
static MalFunction first = new MalFunction() {
public MalVal apply(MalList a) throws MalThrowable {
- MalList ml = ((MalList)a.nth(0));
+ MalVal exp = a.nth(0);
+ if (exp == Nil) {
+ return Nil;
+ }
+ MalList ml = ((MalList)exp);
return ml.size() > 0 ? ml.nth(0) : Nil;
}
};
static MalFunction rest = new MalFunction() {
public MalVal apply(MalList a) throws MalThrowable {
- MalList ml = ((MalList)a.nth(0));
+ MalVal exp = a.nth(0);
+ if (exp == Nil) {
+ return new MalList();
+ }
+ MalList ml = ((MalList)exp);
return ml.rest();
}
};
}
def first(a: List[Any]): Any = {
- val lst = a(0).asInstanceOf[MalList].value
- if (lst.length > 0) lst(0) else null
+ a(0) match {
+ case null => null
+ case ml: MalList => {
+ val lst = ml.value
+ if (lst.length > 0) lst(0) else null
+ }
+ }
}
def rest(a: List[Any]): Any = {
a(0) match {
- case null => true
+ case null => _list()
case ml: MalList => _list(ml.drop(1).value:_*)
}
}