altos/scheme: Add builtin list-tail
authorKeith Packard <keithp@keithp.com>
Thu, 4 Jan 2018 10:25:45 +0000 (02:25 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 4 Jan 2018 10:25:45 +0000 (02:25 -0800)
This is used enough to warrant a builtin, rather than lisp implementation

Signed-off-by: Keith Packard <keithp@keithp.com>
src/scheme/ao_scheme_builtin.c
src/scheme/ao_scheme_builtin.txt

index 0b84a89a2b366bdb67f2cf337fa4dc2a34eac24f..1bfe6942b698dfc8ce6148e860a71d214f14842e 100644 (file)
@@ -230,6 +230,31 @@ ao_scheme_do_list_copy(struct ao_scheme_cons *cons)
        return ao_scheme_cons_poly(new);
 }
 
+ao_poly
+ao_scheme_do_list_tail(struct ao_scheme_cons *cons)
+{
+       ao_poly list;
+       int32_t v;
+
+       if (!ao_scheme_check_argc(_ao_scheme_atom_list2dtail, cons, 2, 2))
+               return AO_SCHEME_NIL;
+       if (!ao_scheme_check_argt(_ao_scheme_atom_list2dtail, cons, 0, AO_SCHEME_CONS, 1))
+               return AO_SCHEME_NIL;
+       list = ao_scheme_arg(cons, 0);
+       v = ao_scheme_arg_int(_ao_scheme_atom_list2dtail, cons, 1);
+       if (ao_scheme_exception)
+               return AO_SCHEME_NIL;
+       while (v > 0) {
+               if (!list)
+                       return ao_scheme_error(AO_SCHEME_INVALID, "%v: ran off end", _ao_scheme_atom_list2dtail);
+               if (!ao_scheme_is_cons(list))
+                       return ao_scheme_error(AO_SCHEME_INVALID, "%v: invalid list", _ao_scheme_atom_list2dtail);
+               list = ao_scheme_poly_cons(list)->cdr;
+               v--;
+       }
+       return list;
+}
+
 ao_poly
 ao_scheme_do_quote(struct ao_scheme_cons *cons)
 {
index 4739f121cab359b07b2c8c5c28aa7e421e01c611..7298add7c42c7639fc55030582057ccb69cae254 100644 (file)
@@ -12,6 +12,7 @@ all   f_lambda        cons
 all    f_lambda        last
 all    f_lambda        length
 all    f_lambda        list_copy       list-copy
+all    f_lambda        list_tail       list-tail
 all    nlambda         quote
 QUASI  atom            quasiquote
 QUASI  atom            unquote