basics-general.mlish (1506B)
1 #lang s-exp "../../../mlish.rkt" 2 3 (define-type (List X) 4 Nil 5 (Cons X (List X))) 6 (define-type (** X Y) 7 (Pair X Y)) 8 (define-type Bool 9 True 10 False) 11 12 (define (map [f : (→ A B)] [x* : (List A)] → (List B)) 13 (match x* with 14 [Nil -> Nil] 15 [Cons x x* -> (Cons (f x) (map f x*))])) 16 17 (define (append [x* : (List A)] [y* : (List A)] → (List A)) 18 (match x* with 19 [Nil -> y*] 20 [Cons x x* -> (Cons x (append x* y*))])) 21 22 (define (fst [xy : (** A B)] → A) 23 (match xy with 24 [Pair x y -> x])) 25 26 (define (snd [xy : (** A B)] → B) 27 (match xy with 28 [Pair x y -> y])) 29 30 (define (member [x* : (List A)] [y : A] → Bool) 31 (match x* with 32 [Nil -> False] 33 [Cons x x* -> 34 (if (equal? x y) True (member x* y))])) 35 36 (define (foldl [f : (→ A B A)] [acc : A] [x* : (List B)] → A) 37 (match x* with 38 [Nil -> acc] 39 [Cons x x* -> (foldl f (f acc x) x*)])) 40 41 (define (foldr [f : (→ A B B)] [x* : (List A)] [acc : B] → B) 42 (match x* with 43 [Nil -> acc] 44 [Cons x x* -> (f x (foldr f x* acc))])) 45 46 (define (filter [f : (→ A Bool)] [x* : (List A)] → (List A)) 47 (foldr (λ ([x : A] [acc : (List A)]) (match (f x) with [True -> (Cons x acc)] [False -> acc])) 48 x* 49 Nil)) 50 51 (define (sum [x* : (List Float)] → Float) 52 (foldl fl+ (exact->inexact 0) x*)) 53 54 (define (reverse [x* : (List A)] → (List A)) 55 (foldl (λ ([x* : (List A)] [x : A]) (Cons x x*)) Nil x*)) 56 57 (provide-type List Nil Cons ** Pair Bool True False) 58 59 (provide map append fst snd member foldl foldr filter sum reverse)