# [plt-scheme] Do Scheme programmers use the 'do' form very often?

 From: John David Stone (stone at cs.grinnell.edu) Date: Thu Feb 14 11:46:22 EST 2008 Previous message: [plt-scheme] ssh/sftp library in mzscheme Next message: [plt-scheme] Running mzc with DrScheme-compatible error-tracing stuff? Messages sorted by: [date] [thread] [subject] [author]

```        I find that I've used do-expressions in earnest about fifty times
over the last fifteen years.  Here are five representative examples:

Computing and displaying "multiples" of a point on an elliptic curve:

===========================================================================
(define multiples
(lambda (point)
(do ((i 0 (+ i 1))
(acc point (pt+ acc point f)))
((= i 12))
(display (cons (exact->inexact (car acc))
(exact->inexact (cdr acc)))) (newline))))
===========================================================================

Command interface for a GUI application:

===========================================================================
(define kaleidoscope
(lambda ()
(do ((command 'continue
(handle-events k-display #f #f (key-press handler))))
((eq? command 'stop) (clean-up-afterwards))
(if (eq? command 'clear)
(clear-area k-window 0 0 diameter-in-pixels diameter-in-pixels
#t))
(draw))))
===========================================================================

Vector traversal:

===========================================================================
(define vector-map
(lambda (proc vec)
(let* ((len (vector-length vec))
(result (make-vector len)))
(do ((index 0 (+ index 1)))
((= index len) result)
(vector-set! result index (proc (vector-ref vec index)))))))
===========================================================================

Gaussian elimination to solve systems of linear equations:

===========================================================================
(define (gaussian-elimination coefficients right-hand-sides)
(let ((n (vector-length right-hand-sides))
(lhs (matrix-copy coefficients))
(rhs (vector-copy right-hand-sides)))

;;; some local procedure definitions elided here

(do ((index 0 (+ index 1)))
((= index (- n 1)))
(let* ((swapfacts (biggest-in-column index index))
(pivot (car swapfacts))
(swap-row (cdr swapfacts)))
(swap-equations! index swap-row)
(do ((eliminand (+ index 1) (+ eliminand 1)))
((= eliminand n))
eliminand
index
(- (/ (\$\$ lhs eliminand index) pivot))))))
(back-substitute lhs rhs)))
===========================================================================