094-when.scm (958B)
1 ; (when test body...) -- if test is non-#f, eval body and return last; 2 ; otherwise return UNSPEC. The body forms execute left-to-right. 3 4 ;; --- Truthy test: body runs, last form is the value ------------------ 5 (if (= 5 (when #t 1 2 3 4 5)) 0 (sys-exit 1)) 6 7 ;; --- Falsy test: body skipped; result is UNSPEC (eq? to other UNSPECs) 8 (if (eq? (when #f (sys-exit 99)) (when #f 42)) 0 (sys-exit 2)) 9 (if (not (eq? (when #f 1) #t)) 0 (sys-exit 3)) 10 (if (not (eq? (when #f 1) #f)) 0 (sys-exit 4)) 11 (if (not (eq? (when #f 1) '())) 0 (sys-exit 5)) 12 13 ;; --- Body side-effects fire only when test is truthy ----------------- 14 (define counter 0) 15 (when #f (set! counter 100)) 16 (if (= counter 0) 0 (sys-exit 6)) 17 (when #t (set! counter (+ counter 1)) (set! counter (+ counter 2))) 18 (if (= counter 3) 0 (sys-exit 7)) 19 20 ;; --- Truthy non-#t value (any non-#f counts as true) ----------------- 21 (if (= 7 (when 0 7)) 0 (sys-exit 8)) 22 (if (= 7 (when '() 7)) 0 (sys-exit 9)) 23 24 (sys-exit 42)