PasteRack.org | ||
Paste # 67020 | ||
2020-02-17 10:55:05 | ||
Fork as a new paste. | ||
Paste viewed 379 times. | ||
Tweet | ||
Embed: | ||
#lang racket/base ;; A debug print syntax, including printing only when a condition or ;; counter is reached. ;; ;; The counter shows the use of `syntax-local-lift-expression` to make ;; a definition not at the invocation site, but up at the module ;; level. (require (for-syntax racket/base syntax/parse)) (define-syntax (dbg stx) (syntax-parse stx [(_ e:expr ...+) #'(begin (printf "`~a`=~v " 'e e) ... (newline))] [(_ #:when condition:expr e:expr ...+) #'(when condition (printf "[#:when `~a`] " 'condition) (dbg e ...))] [(_ #:count goal:nat e:expr ...+) #:with count (syntax-local-lift-expression #'0) #'(begin (when (= goal count) (printf "[#:count ~v] " count) (dbg e ...)) (set! count (add1 count)))])) ;; Example use (for ([s '("hi" "there" "blah" "foo" "bar")] [n (in-naturals)]) (dbg #:when (= 2 n) n s) ;; Similar, handier when you don't already have some counter: (dbg #:count 2 s))