;;;; match over numbers, chars, strings and dyn values: each arm is (= t lit) ;;;; over one temporary, and a _ arm is the rest. (defn small [n i16] string (match n 5 "five" -3 "minus three" _ "other")) (defn half [x f32] i32 (match x 0.5 1 2 2 _ 0)) (defn letter [c u8] i32 (match c \a 1 98 2 _ 0)) (defn command [s string] i32 (match s "go" 1 "stop" 2 "" 3 _ 0)) (defn big [n u64] i32 (match n 18446744073709551615 1 _ 0)) ;; Over a dyn the test is dyn =, so 1 matches 1.0 and "go" matches only a ;; string. (defn kind [d dyn] string (match d 1 "one" 2.5 "two and a half" "go" "go" _ "other")) (defn calls [] i32 (print "(called) ") 7) ;; recur from inside an arm: the arm is the loop's tail. (defn count-down [from i32] i32 (loop [n from steps 0] (match n 0 steps _ (recur (- n 1) (+ steps 1))))) (defn main [] i32 (println (small 5)) (println (small -3)) (println (small 4)) (print (half 0.5)) (print (half 2.0)) (print (half 3.0)) (println "") (print (letter 97)) (print (letter 98)) (print (letter 99)) (println "") (print (command "go")) (print (command "stop")) (print (command "")) (print (command "gone")) (println "") (print (big 18446744073709551615)) (print (big 1)) (println "") (println (kind 1)) (println (kind 1.0)) (println (kind 2.5)) (println (kind "go")) (println (kind "1")) ;; The scrutinee is evaluated once, however many arms test it. (println (match (calls) 1 "a" 2 "b" 7 "seven" _ "c")) (print (count-down 4)) (println "") 0)