aboutsummaryrefslogtreecommitdiffstats
path: root/list.csc
blob: c4bc013d015bff54459779d407a30a243b6f0527 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(define-library (csc list)
  (export
    revappend
    split-at
    take)
  (import (scheme base))
  (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)))))


    (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)))))


    (define (revappend a b)
      (let loop ((xs a)
                 (acc b))
        (if (null? xs)
            acc
          (loop (cdr xs) (cons (car xs) acc)))))))