47 lines
1.0 KiB
Plaintext
47 lines
1.0 KiB
Plaintext
;;;; match over an enum: the arms name members as keywords, a _ arm is the
|
|
;;;; rest, and a match that names neither every member nor _ is refused.
|
|
|
|
(defenum Dir [north east south west])
|
|
|
|
(defn turn [d Dir] Dir
|
|
(match d
|
|
:north :east
|
|
:east :south
|
|
:south :west
|
|
:west :north))
|
|
|
|
(defn name [d Dir] str
|
|
(match d
|
|
:north "north"
|
|
:south "south"
|
|
_ "sideways"))
|
|
|
|
(defn calls [] i32
|
|
(print "(called) ")
|
|
0)
|
|
|
|
;; The scrutinee is evaluated once, however many arms test it.
|
|
(defn once [] Dir
|
|
(calls)
|
|
:west)
|
|
|
|
;; break from inside an arm leaves the while around the match.
|
|
(defn steps-to-west [from Dir] i32
|
|
(let [d from n 0]
|
|
(while true
|
|
(match d
|
|
:west (break)
|
|
_ (do (set d (turn d)) (set n (+ n 1)))))
|
|
n))
|
|
|
|
(defn main [] i32
|
|
(print (steps-to-west :north)) (println "")
|
|
(println (name :north))
|
|
(println (name (turn :north)))
|
|
(println (name (turn (turn :north))))
|
|
(println (name (once)))
|
|
(match (turn :west)
|
|
:north (println "back to north")
|
|
_ (println "somewhere else"))
|
|
0)
|