Guarded Evaluation: when and unless

Guarded Evaluation: when and unless

whenunless는 조건이 참일 때만 본문을 실행하는 구문 폼이에요. The Racket Guide의 Effects If...: when and unless에서 whenunless를 소개하고 있어요.

출처: Racket Reference

본문

(when test-expr body ...+)

syntax

test-expr를 평가해요. 결과가 #f이면 when 표현식의 결과는 #<void>예요. 그렇지 않으면 body들이 평가되고, 마지막 bodywhen 폼에 대해 꼬리 위치에 있어요.

> (when (positive? -5)
    (display "hi"))
> (when (positive? 5)
    (display "hi")
    (display " there"))
hi there
(unless test-expr body ...+)

syntax

(when (not test-expr) body ...+)와 동등해요.

> (unless (positive? 5)
    (display "hi"))
> (unless (positive? -5)
    (display "hi")
    (display " there"))
hi there

더 알아보기