[ prog / sol / mona ]

prog


Post macros.

1 2018-11-04 22:12

Post your dirty little macros.

(define-macro (reverse-args function . args)
  `(,function ,@(reverse args)))

(define-macro (thunk body0 . body) `(λ () ,body0 ,@body))

(define-macro (maximize! place number)
  `(set! ,place (max ,place ,number)))

(define-macro (minimize! place number)
  `(set! ,place (min ,place ,number)))

(define-macro (withret . body)
  `((λ () (call/cc (λ (return) ,@body)))))

here are some more from my common-lisp.scm

;prog1
(define-syntax begin0
  (syntax-rules ()
    ((begin0 expr0 expr+ ...)
     (call-with-values (λ () expr0)
       (lambda return
         (begin expr+ ... (apply values return)))))))

(define-macro (setf! . assignments)
  `(begin ,@(map (λ (a) (cons 'set! a)) assignments)))

(define-macro (push! x place)
  `(set! ,place (cons ,x ,place)))

(define-macro (pop! place)
  `(begin0 (car ,place) (set! ,place (cdr ,place))))

(define-syntax incf!
  (syntax-rules ()
    ((incf! place) (incf! place 1))
    ((incf! place n)
     (set! place (+ place n)))))

(define-syntax decf!
  (syntax-rules ()
    ((decf! place) (decf! place 1))
    ((decf! place n) (incf! place (- n)))))

(define-syntax dolist
  (syntax-rules ()
    ((dolist (var list) body body+ ...)
     (dolist (var list (values)) body body+ ...))
    ((dolist (var list value) body body+ ...)
     (begin (for-each (λ (var) body body+ ...) list) value))))

(define-syntax dotimes
  (syntax-rules ()
    ((dotimes (variable n) body body+ ...)
     (dotimes (variable n (values)) body body+ ...))

    ((dotimes (variable n value) body body+ ...)
     (do ((variable 0 (1+ variable)))
         ((= variable n) value)
       body body+ ...))))
40


VIP:

do not edit these