From 9a699da5c5ff8fc2a4908bce5e29a0641503ce89 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 11 Jul 2016 00:21:45 +0200 Subject: [PATCH] add length. --- TODO | 2 +- mes.c | 12 ++++++++++++ test.mes | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index 399e3a1f..7a323edf 100644 --- a/TODO +++ b/TODO @@ -15,7 +15,7 @@ v "string" assq call-with-values v char? -length +v length v list list->vector make-vector diff --git a/mes.c b/mes.c index d02b0ba6..7a255080 100644 --- a/mes.c +++ b/mes.c @@ -560,6 +560,18 @@ string_length (scm *x) return make_number (strlen (x->name)); } +scm * +length (scm *x) +{ + int n = 0; + while (x != &scm_nil) + { + n++; + x = cdr (x); + } + return make_number (n); +} + scm * lookup (char *x, scm *a) { diff --git a/test.mes b/test.mes index 2eff4e96..f1215dc8 100644 --- a/test.mes +++ b/test.mes @@ -182,4 +182,11 @@ (display (string #\a #\space #\s #\t #\r #\i #\n #\g #\newline)) (newline) +(display "length of nil: ") +(display (length '())) +(newline) +(display "length of '(a b c): ") +(display (length '(a b c))) +(newline) + '()