From a54f483b1f9ae3f03f1f00124eec296c2d9218d7 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 22 Jan 2022 17:51:20 -0800 Subject: Add a loop macro. --- csc/list.csc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'csc/list.csc') diff --git a/csc/list.csc b/csc/list.csc index f425146..dcc1766 100644 --- a/csc/list.csc +++ b/csc/list.csc @@ -7,27 +7,26 @@ split-at take) (import (scheme base) + (only (csc loop) + loop + return) (only (csc match) match)) (begin (define (take n xs) - (let loop ((n n) - (xs xs) - (acc '())) - (if (or (not (positive? n)) (null? xs)) - (reverse acc) - (loop (- n 1) (cdr xs) (cons (car xs) acc))))) + (loop for x in xs + for i from 1 to n + collect x)) (define (split-at n xs) - (let loop ((n n) - (xs xs) - (acc '())) - (if (or (not (positive? n)) (null? xs)) - (values (reverse acc) xs) - (loop (- n 1) (cdr xs) (cons (car xs) acc))))) + (loop for i from 1 to n + for x in xs + collect x into first-half + for second-half = xs then (cdr second-half) + finally (return (values first-half second-half)))) (define (revappend a b) -- cgit v1.3.1