조건문
tutorial point에는 다섯개의 조건문을 소개한다.
if if/do nested if case cond
if
user=> (defn one? [x]
#_=> (if (= x 1)
#_=> (println "EQUAL!!!!")
#_=> (println "no...")))
#'user/one?
user=> (one? 1)
EQUAL!!!!
nil
if/do
if문의 큰 단점이 있는데 그것은 하나만 할 수 있다는 것이다. 그래서 do를 붙인다. 일단 보자.
user=> (defn two? [x]
#_=> (if (= x 2)
#_=> (do (println "TWO!!!!")
#_=> (one? x))
#_=> (do (println "not Two")
#_=> (one? x))))
#'user/two?
user=> (two? 1)
not Two
EQUAL!!!!
nil
여러 개의 일을 do안에 넣는 것이다.
nested if
(defn Example [] (
if ( and (= 2 2) (= 3 3))
(println "Values are equal")
(println "Values are not equal")))
(Example)
그냥 and를 사용할 수 있다는 점을 기억하기 바란다.
case
case expression
value1 statement #1
value2 statement #2
valueN statement #N
statement #Default
case문이라고 생각하면 될듯하다.
user=> (defn score [x]
#_=> (case x
#_=> 10 (println "score 10")
#_=> (println "not 10")))
#'user/score
user=> (score 10)
score 10
nil
cond
cond
(expression evaluation1) statement #1
(expression evaluation2) statement #2
(expression evaluationN) statement #N
:else statement #Default
그냥 condition이라 생각하면 편하다
(defn Example []
(def x 5)
(cond
(= x 5) (println "x is 5")
(= x 10)(println "x is 10")
:else (println "x is not defined")))
(Example)
댓글 없음:
댓글 쓰기