[plt-scheme] stupid Swindle tricks

From: Doug Orleans (dougorleans at gmail.com)
Date: Fri May 26 01:02:19 EDT 2006

Candidates for swindle/misc.ss, in decreasing order of usefulness:

  (defsyntax* set-if!
    (syntax-rules ()
      ((_ condition then-place else-place val)
       (if condition (set! then-place val) (set! else-place val)))))

  (defsyntax* last-of
    (syntax-rules ()
      ((_ successor first clause ...)
       (collect (x first (successor x)) clause ...))))

  (define* (nth-of n successor first)
    (last-of successor first (i <- 0 ..< n)))

  (defsyntax* set-nth-of!
    (syntax-rules ()
      ((_ n successor first val)
       (let ((nn n))
	 (set! (if (zero? nn)
		   first
		   (successor (nth-of (1- nn) successor first)))
	       val)))))

  (defsyntax* push-at-end!
    (syntax-rules ()
      ((_ val place)
       (set! (nth-of (length place) cdr place) (list val)))))

  (defsyntax* pop-at-end!
    (syntax-rules ()
      ((_ place)
       (begin0 (last place)
	       (set! (nth-of (1- (length place)) cdr place) null)))))


--dougorleans at gmail.com


Posted on the users mailing list.