From Newsgroup: comp.lang.scheme
Alex Mizrahi wrote:
for example, simple function that sums elements in the vector:
(defun jspank (a)
(declare (optimize (speed 3) (safety 0))
(type (vector single-float) a))
(let ((x 0.0))
(declare (single-float x))
(dotimes (i (length a))
(declare (fixnum i))
(setf x (+ x (aref a i))))))
(define (sumvec vec)
(with-gen (x vec)
(do ((sum 0 (+ sum (x))))
((x-null?) sum))))
(sumvec #(1 3 5 7 9))
===>
25
Given:
;; Make a vector generator.
(define (gmk vec)
(let ((v vec)
(i 0))
(values
(lambda()
(if (= i (vector-length v)) #f (begin0 (~ v i) (inc! i))))
;; Tells if generator is exhausted.
(lambda() (= i (vector-length v))))))
(define-macro with-gen
(lambda (args . body)
(define duos
(let go ()
(if (null? args) '() (cons (list (pop! args)(pop! args)) (go)))))
(define (proc s v)
`((,s ,(symbol-append s '-null?)) (gmk ,v)))
`(let-values
(,@(map (lambda (xs) (apply proc xs)) duos))
,@body)))
--
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2