There are a few more flow control concepts that we need to cover.
The cond function allows a series of tests and expressions with each test evaluated in order and the first true expression returned. A small additional details in the example is the let function which binds a name to a value. Lets look at an example:
(let [x 7]
(cond
(< x 3) "x is less than 3"
(< x 7) "x is less than 7"
(< x 10) "x is less than 10"
(< x 20) "x is less than 20"))
=> "x is less than 10"
You can even add an else statement in the event of no true expressions
(let [x 7]
(cond
(< x 3) "x is less than 3"
(< x 7) "x is less than 7"
:else "x is greater than or equal to 7"))
=> "x is greater than or equal to 7"
Then there are case expressions which are implemented as follows
( let [x 7]
(case x
7 (println "x is 7")
10 (println "x is 10")
(println "x is neither 7 nor 10")))
x is 7
=> nil
Tomorrow I will look at iterations and recursion.
(println “Bye 4 Now!”)
No comments:
Post a Comment