BANNER
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

(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)
	#f
	(if (equal? obj (car (car al)))
	    (car al)
	    (loop (cdr al))))))

caddr

(caddr xs)

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

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

cadr

(cadr xs)

Returns the car of the cdr of xs.

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

cddr

(cddr xs)

Returns the cdr of the cdr of xs.

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

even?

(even? n)

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

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

expt

(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)
                        1
                        (* b (expt1 (- n 1)))))))
    (if (< n 1)
        (/ (expt1 (- n)))
        (expt1 n))))

list->string

(list->string cs)

Returns a string of the characters in cs.

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

map

(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)))
           (else
            (let loop ((xs xs))
              (if (null? (car xs))
                  '()
                  (cons (apply f (map1 car xs))
                        (loop (map1 cdr xs)))))))))

odd?

(odd? n)

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

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

string->list

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

(zero? n)

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

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

PrevHomeNext
Introduction Derived Procedures [10.2]