Returns #t if predicate pred? returns true when applied to any element of the list xs.
;; Return #t if predicate `pred?' returns #t when applied to any
;; element of the `xs'.
(define (any? pred? xs)
(let loop ((xs xs))
(and (not (null? xs))
(or (pred? (car xs))
(loop (cdr xs)))))) |
Returns a list of the objects in alist as an associative list.
(define (assoc-objs alist)
;; Returns a list of the objects in an associative list.
;; (("a" "b") ("c" "d")) => ("a" "c")
(let loop ((result '()) (al alist))
(if (null? al)
result
(loop (append result (list (car (car al)))) (cdr al))))) |
Returns
;; Like `span', but with the sense of the test reversed.
(define (break test? xs)
(span (compose not test?) xs)) |
Returns the list xs less the first n elements.
;; Return list `xs' less the first `n' elements.
(define (drop n xs)
(list-tail xs n)) |
Remove leading elements of list xs for which test? returns true.
;; Remove leading elements of list `xs' for which `test?' returns
;; true.
(define (dropwhile test? xs)
(cond ((null? xs)
'())
((test? (car xs))
(dropwhile test? (cdr xs)))
(else xs))) |
(list-member-find element element-list) |
Returns the offset within element-list of element, or -1 if element is not a member of element-list.
The offset of the first element in element-list is 0. Occurence of element is tested with equal?, so the elements may be objects of any time for which equal? will work.
(define (list-member-find element element-list)
;; Finds element in element-list and returns the index of its location.
;; The first element in a list has index 0.
(let loop ((elemlist element-list) (count 0))
(if (null? elemlist)
-1
(if (equal? element (car elemlist))
count
(loop (cdr elemlist) (+ count 1)))))) |
(list-member-get element-list count) |
Returns the element at offset count within element-list, or #f if element-list does not contain count elements.
(define (list-member-get element-list count)
;; Returns the count'th element from element-list.
;; The first element in a list has index 0.
(let loop ((elemlist element-list) (idx count))
(if (null? elemlist)
#f
(if (= idx 0)
(car elemlist)
(loop (cdr elemlist) (- idx 1)))))) |
Returns list ys without any elements equalling x.
;; Remove any occurrences of `x' from list `ys'.
(define (remove x ys)
(cond
((null? ys) ys)
((equal? x (car ys)) (remove x (cdr ys)))
(else (cons (car ys) (remove x (cdr ys)))))) |
Returns list ys without any elements for which predicate pred? returns #t.
;; Remove any elements `x' from that answer #t to `pred?'.
(define (remove-if pred? ys)
(cond
((null? ys) ys)
((pred? (car ys)) (remove-if pred? (cdr ys)))
(else (cons (car ys) (remove-if pred? (cdr ys)))))) |
Returns list l sorted using comparison function <=.
;; O'Keefe's smooth applicative merge sort: sort list `l' using
;; comparison function `<='.
(define (sort <= l)
(letrec ((merge (lambda (xs ys)
(cond ((null? xs) ys)
((null? ys) xs)
(else (if (<= (car xs)
(car ys))
(cons (car xs)
(merge (cdr xs) ys))
(cons (car ys)
(merge xs (cdr ys))))))))
(mergepairs (lambda (l k)
(if (null? (cdr l))
l
(if (= 1 (modulo k 2))
l
(mergepairs (cons (merge (car l)
(cadr l))
(cddr l))
(quotient k 2))))))
(sorting (lambda (l a k)
(if (null? l)
(car (mergepairs a 0))
(sorting (cdr l)
(mergepairs (cons (list (car l))
a)
(+ k 1))
(+ k 1))))))
(cond ((not (list? l))
(error "sort: second arg not a list"))
((not (procedure? <=))
(error "sort: first arg not a procedure"))
((null? l)
'())
(else
(sorting l '() 0))))) |
Returns a pair of lists, one is the leading elements of the list xs for which test? returns #t, and the other is the rest of the elements of xs.
;; From the list `xs', return a pair of lists comprising the leading
;; elements of `xs' for which `test?' returns true and the rest of
;; `xs'. After the Haskell prelude.
(define (span test? xs)
(if (null? xs)
(cons '() '())
(let ((x (car xs)) ; split the xs into head
(xss (cdr xs))) ; and tail
(if (test? x)
(let* ((spanned (span test? xss))
;; and split the result of span into head and tail
(ys (car spanned))
(zs (cdr spanned)))
(cons (cons x ys) zs))
(cons '() xs))))) |
Returns the first n elements of list xs.
(define (take n xs)
(let loop ((i 1) (xs xs))
(if (or (> i n)
(null? xs))
'()
(cons (car xs)
(loop (+ 1 i) (cdr xs)))))) |
;; List zipping with the given `zipper' function. Like `map', but the
;; list args can be unequal lengths.
(define (zip-with zipper #!rest xs)
(if (any? null? xs)
'()
(cons (apply zipper (map car xs) )
(apply zip-with zipper (map cdr xs))))) |