patternMatching.tri (1008B)
1 !import "patterns" !Local 2 3 -- We can do conditional pattern matching by providing a list of lists, where 4 -- each sublist contains a boolean expression and a function to return if said 5 -- boolean expression evaluates to true. 6 7 value = 42 8 main = match value [[(equal? "Hello") (_ : ", world!")] [(equal? 42) (_ : "The answer.")]] 9 10 -- < main 11 -- > "The answer." 12 13 matchExample = (x : match x 14 [[(equal? 1) (_ : "one")] 15 [(equal? 2) (_ : "two")] 16 [(equal? 3) (_ : "three")] 17 [(equal? 4) (_ : "four")] 18 [(equal? 5) (_ : "five")] 19 [(equal? 6) (_ : "six")] 20 [(equal? 7) (_ : "seven")] 21 [(equal? 8) (_ : "eight")] 22 [(equal? 9) (_ : "nine")] 23 [(equal? 10) (_ : "ten")] 24 [ otherwise (_ : "I ran out of fingers!")]]) 25 26 -- < matchExample 3 27 -- > "three" 28 -- < matchExample 5 29 -- > "five" 30 -- < matchExample 9 31 -- > "nine" 32 -- < matchExample 11 33 -- > "I ran out of fingers!" 34 -- < matchExample "three" 35 -- > "I ran out of fingers!" 36 -- < matchExample [("hello") ("world")] 37 -- > "I ran out of fingers!"