Scheme Programming Idioms

Life is hard. Can't get to heaven on roller skates.
-Timbuk Three
This document is designed not to describe the function of particular Scheme functions or forms, but rather to explain how to use them to program particular things. Each programming language has its own idioms, patterns of code used to express some standard kind of computation.

This code Can be replaced by this
(if test #t #f) test
(if test #f #t) (not test)
(if foo bar #f) (and foo bar)
(if foo #t bar) (or foo bar)
(if test x (+ x y)) (+ x (if test y 0))
(if test (word w '-suffix) w) (word w (if test '-suffix ""))
(every (lambda (x)
          (if (
test? x) x '()))
       
sen)
(keep test? sen)
(if test (begin foo bar)) (cond (test foo bar))
(lambda (x) (proc x)) proc
(cond ((equal? letter 'a) #t)
      ((equal? letter 'e) #t)
      ((equal? letter 'i) #t)
      ((equal? letter 'o) #t)
      ((equal? letter 'u) #t)
      (else #f)))
(or (equal? letter 'a)
    (equal? letter 'e)
    (equal? letter 'i)
    (equal? letter 'o)
    (equal? letter 'u))
(or (equal? letter 'a)
    (equal? letter 'e)
    (equal? letter 'i)
    (equal? letter 'o)
    (equal? letter 'u))
(member? letter '(a e i o u))
(= (count foo) 0) (empty? foo)
(= (count foo) 1) (empty? (butfirst foo))

Send me more and I'll put them in.


Barak Pearlmutter <bap@cs.unm.edu>