IF 특수 연산자

IF 특수 연산자 (if)

if는 Common Lisp의 특수 연산자로, 하나의 테스트 폼(test form)의 결과에 따라 실행할 폼을 골라요. 조건 분기의 가장 기본형으로, condwhen/unless의 바탕이 되는 구조예요.

출처: CLHS: Special Operator IF

문법 (Syntax)

if test-form then-form [else-form] => result*

인자와 값 (Arguments and Values)

  • test-form — 폼이에요.
  • then-form — 폼이에요.
  • else-form — 폼이며, 기본값은 nil이에요.
  • resultstest-form이 참(true)이면 then-form이 돌려준 값들이고, 그렇지 않으면 else-form이 돌려준 값들이에요.

설명 (Description)

if는 하나의 test-form에 폼 실행이 달리게 해요.

먼저 test-form을 평가해요. 결과가 참이면 then-form을, 아니면 else-form을 선택해요. 선택된 폼을 그다음에 평가해요.

예제 (Examples)

 (if t 1) => 1
 (if nil 1 2) => 2 
 (defun test ()
   (dolist (truth-value '(t nil 1 (a b c)))
     (if truth-value (print 'true) (print 'false))
     (prin1 truth-value))) => TEST
 (test)
>> TRUE T
>> FALSE NIL
>> TRUE 1
>> TRUE (A B C)
=> NIL

NIL을 제외한 모든 값이 참으로 취급되는 걸 볼 수 있어요. 빈 리스트 (A B C)도 참이에요.

영향 (Affected By)

없음.

예외 상황 (Exceptional Situations)

없음.

함께 보기 (See Also)

  • cond, unless, when

참고 (Notes)

 (if test-form then-form else-form)
 == (cond (test-form then-form) (t else-form))

더 알아보기 (Learn more)