tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

82199ce4c4ba4c014c2cd09bdcc0644732c2f6863c0eff2710f4156dcb50a1e5.tri (1029B)


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