listpats.mlish (1359B)
1 #lang s-exp "../../mlish.rkt" 2 (require "../rackunit-typechecking.rkt") 3 4 ;; pattern matching for built-in lists 5 6 (check-type 7 (match (list 1 2) with 8 [[] -> 0] 9 [[x y] -> (+ x y)]) : Int -> 3) 10 11 (typecheck-fail 12 (match (list 1 2) with 13 [[x y] -> (+ x y)]) #:with-msg "missing empty list case") 14 15 (typecheck-fail 16 (match (list 1 2) with 17 [[] -> 0]) #:with-msg "missing non\\-empty list case") 18 19 (check-type 20 (match (list 1 2) with 21 [[] -> 0] 22 [[x y] -> (+ x y)]) : Int -> 3) 23 24 (check-type 25 (match (list 1 2) with 26 [[x y] -> (+ x y)] 27 [[] -> 0]) : Int -> 3) 28 29 (check-type 30 (match (nil {Int}) with 31 [[x y] -> (+ x y)] 32 [[] -> 0]) : Int -> 0) 33 34 (check-type 35 (match (list 1 2 3) with 36 [[] -> nil] 37 [x :: rst -> rst]) : (List Int) -> (list 2 3)) 38 39 (check-type 40 (match (list 1 2 3) with 41 [[] -> nil] 42 [x :: y :: rst -> rst]) : (List Int) -> (list 3)) 43 44 (check-type 45 (match (nil {Int}) with 46 [[] -> nil] 47 [x :: y :: rst -> rst]) : (List Int) -> nil) 48 49 (check-type 50 (match (list 1 2 3) with 51 [[] -> 0] 52 [x :: y :: rst -> (+ x y)]) : Int -> 3) 53 54 (check-type 55 (match (list 1 2 3) with 56 [[] -> 0] 57 [[x] -> 2] 58 [x :: rst -> 3]) : Int -> 3) 59 60 (check-type 61 (match (list 1) with 62 [[] -> 0] 63 [[x] -> 2] 64 [x :: rst -> 3]) : Int -> 2) 65 66 (check-type 67 (match (list 1) with 68 [[] -> 0] 69 [x :: rst -> 3] 70 [[x] -> 2]) : Int -> 3)