Chapter 1. ISO/IEC 10179 Procedures

Standard Procedures [8.5]

assoc

Norman Walsh

Revision History
Revision 1.019971022Norman Walsh norm@berkshire.net
Submitted in private mail 19971022. Replaces version submitted by Dave Love.
(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

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(caddr xs)

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

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

cadr

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(cadr xs)

Returns the car of the cdr of xs.

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

cddr

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(cddr xs)

Returns the cdr of the cdr of xs.

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

even?

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(even? n)

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

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

expt

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(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

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(list->string cs)

Returns a string of the characters in cs.

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

map

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(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?

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(odd? n)

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

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

string->list

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(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?

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(zero? n)

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

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

Derived Procedures [10.2]

ifollow

Norman Walsh

Revision History
Revision 1.019971022Norman Walsh norm@berkshire.net
Submitted in private mail 19971022
(ifollow nl)

Returns the node immediately following the nl node list.

(define (ifollow nl)
  (node-list-map (lambda (snl)
		   (let loop ((rest (siblings snl)))
		     (cond ((node-list-empty? rest)
			    (empty-node-list))
			   ((node-list=? (node-list-first rest) snl)
			    (node-list-first (node-list-rest rest)))
			   (else
			    (loop (node-list-rest rest))))))
		 nl))

ipreced

Norman Walsh

Revision History
Revision 1.019971022Norman Walsh norm@berkshire.net
Submitted in private mail 19971022
(ipreced nl)

Returns the node immediately preceding the nl node list.

(define (ipreced nl)
  (node-list-map (lambda (snl)
		 (let loop ((prev (empty-node-list))
			    (rest (siblings snl)))
		   (cond ((node-list-empty? rest)
			  (empty-node-list))
			 ((node-list=? (node-list-first rest) snl)
			  prev)
			 (else
			  (loop (node-list-first rest)
				(node-list-rest rest))))))
		 nl))

ipreced

William Lindsay

Revision History
Revision 1.019970728 
(ipreced nl)

Returns the preceding sibling element or empty node list if this is the first child.

(define (ipreced nl)
  (node-list-map (lambda (snl)
                   (let loop ((prev (empty-node-list))
                              (rest (siblings snl)))
                     (cond ((node-list-empty? rest)
                            (empty-node-list))
                           ((node-list=? (node-list-first rest) snl)
                            prev)
                           (else
                            (loop (node-list-first rest)
                                  (node-list-rest rest))))))
                 nl))

node-list-filter

Chris Maden

Revision History
Revision 1.019971021Chris Maden crism@ora.com
Copied from author's DSSSList post of 19971017
(node-list-filter proc nl)

This procedure is defined in the DSSSL standard. Returns a node-list containing just those members of nl for which proc applied to a singleton node-list containing just that member does not return #f.

The DSSSL standard (ISO/IEC 10179) implementation of node-list-filter reverses the order of the nodes passed to it. This altered version keeps the nodes in order.

;; (node-list-filter) from ISO/IEC 10179 with small change to avoid
;; reversing the order of nodes
(define (node-list-filter proc nl)
  (node-list-reduce nl
        (lambda (result snl)
           (if (proc snl)
               (node-list result snl)
               result))
           (empty-node-list)))

node-list-last

Norman Walsh

Revision History
Revision 1.019971022Norman Walsh norm@berkshire.net
Submitted in private mail 19971022
(node-list-last nl)

Returns the last node in the nl node list.

(define (node-list-last nl)
  (node-list-ref nl
		 (- (node-list-length nl) 1)))

node-list-length

Norman Walsh

Revision History
Revision 1.019971022Norman Walsh norm@berkshire.net
Submitted in private mail 19971022
(node-list-length nl)

Returns the number of nodes in the nl node list.

(define (node-list-length nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (+ result 1))
                    0))

node-list-reduce

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(node-list-reduce nl combine init)

If the node-list nl is empty, returns init. If not, returns the result of applying node-list-reduce to:

  • a node-list containing all but the first member of nl

  • combine, and

  • the result of applying combine to init and the first member of nl.

(define (node-list-reduce nl combine init)
  (if (node-list-empty? nl)
      init
      (node-list-reduce (node-list-rest nl)
                        combine
                        (combine init (node-list-first nl)))))

node-list-reduce

Norman Walsh

Revision History
Revision 1.019971022Norman Walsh norm@berkshire.net
Submitted in private mail 19971022
(node-list-reduce nl proc init)

(define (node-list-reduce nl proc init)
  (if (node-list-empty? nl)
      init
      (node-list-reduce (node-list-rest nl)
                        proc
                        (proc init (node-list-first nl)))))

node-list-some?

Dave Love

Revision History
Revision 1.019970716Dave Love d.love@dl.ac.uk
Copied from David's DSSSList post of 19970702
(node-list-some? proc nl)

Returns the node-list comprising the elements of node-list nl for which procedure proc returns #t.

(define (node-list-some? proc nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (if (or result (proc snl))
                          #t
                          #f))
                    #f))