From 18d143aa6239266cf3bff522289877ff817cc861 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Tue, 24 Apr 2018 07:00:35 +0200 Subject: [PATCH] core: last_pair: Move to core. * src/lib.c (last_pair): New function. * module/mes/scm.mes (last-pair): Remove. --- module/mes/scm.mes | 5 ----- src/lib.c | 8 ++++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/module/mes/scm.mes b/module/mes/scm.mes index 23d4bf53..3aaac56d 100644 --- a/module/mes/scm.mes +++ b/module/mes/scm.mes @@ -157,11 +157,6 @@ (if (= 0 n) x (list-tail (cdr x) (- n 1)))) -(define (last-pair lst) - (let loop ((lst lst)) - (if (or (null? lst) (null? (cdr lst))) lst - (loop (cdr lst))))) - (define (iota n) (if (<= n 0) '() (append2 (iota (- n 1)) (list (- n 1))))) diff --git a/src/lib.c b/src/lib.c index 7b028d5d..18045908 100644 --- a/src/lib.c +++ b/src/lib.c @@ -331,3 +331,11 @@ equal2_p (SCM a, SCM b) } return eq_p (a, b); } + +SCM +last_pair (SCM x) +{ + while (x != cell_nil && CDR (x) != cell_nil) + x = CDR (x); + return x; +}