PrevDSSSL Documentation Project Procedures LibraryNext

Chapter 1. ISO/IEC 10179 Procedures

Table of Contents
Standard Procedures [8.5]
Derived Procedures [10.2]

Standard Procedures [8.5]


(assoc obj alist)

Returns the pair from the alist associative list that has obj as its car, otherwise returns #f if no such pair exists.

(define (assoc obj alist)
  ;; Given an associative list, returns the pair that has obj as a car
  ;; or #f if no such pair exists
  ;; (("a" "b") ("c" "d")), "a" => ("a" "b")
  (let loop ((al alist))
    (if (null? al)
	(if (equal? obj (car (car al)))
	    (car al)
	    (loop (cdr al))))))


(caddr xs)

Returns the car of the cdr of the cdr of xs.

(define (caddr xs)
  (list-ref xs 2))


(cadr xs)

Returns the car of the cdr of xs.

(define (cadr xs)
  (list-ref xs 1))


(cddr xs)

Returns the cdr of the cdr of xs.

(define (cddr xs)
  (cdr (cdr xs)))


(even? n)

Returns #t if n is even, #f otherwise.

(define (even? n)
  (zero? (remainder n 2)))


(expt b n)

Returns b raised to the n power.

(define (expt b n)                      ; safe for -ve n, c.f. Bosak
  (letrec ((expt1 (lambda (n)
                    (if (zero? n)
                        (* b (expt1 (- n 1)))))))
    (if (< n 1)
        (/ (expt1 (- n)))
        (expt1 n))))


(list->string cs)

Returns a string of the characters in cs.

(define (list->string cs)
  (apply string cs))


(map f xs)

Returns a list of results from applying procedure f to corresponding elements of the lists making up the remainder of the arguments to the map procedure. The lists must be of the same length, and f must accept as many arguments as there are lists.

(define (map f #!rest xs)
   (let ((map1 (lambda (f xs)           ; bootstrap version for unary F
                 (let loop ((xs xs))
                   (if (null? xs)
                       (cons (f (car xs))
                             (loop (cdr xs))))))))
     (cond ((null? xs)
           ((null? (cdr xs))
            (map1 f (car xs)))
            (let loop ((xs xs))
              (if (null? (car xs))
                  (cons (apply f (map1 car xs))
                        (loop (map1 cdr xs)))))))))


(odd? n)

Returns #t if n is odd, #f otherwise.

(define (odd? n)
  (not (even? n)))


(string->list s)

Returns a list of the characters in s.

(define (string->list s)
  (let ((l (string-length s)))
    (let loop ((i 0))
      (if (= i l)
          (cons (string-ref s i)
                (loop (+ i 1)))))))


(zero? n)

Returns #t if n is zero, #f otherwise.

(define (zero? n)
  (equal? 0 n))

Introduction Derived Procedures [10.2]