implement find example

This commit is contained in:
AlexKnauth 2016-03-10 14:30:43 -05:00
parent fe494c6ad3
commit db1da5ae20
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,33 @@
#lang s-exp "../../mlish.rkt"
(require "../rackunit-typechecking.rkt")
(define-type (List X)
Nil
(Cons X (List X)))
(define-type (Option X)
None
(Some X))
(define (find [lst : (List X)] [pred : (→ X Bool)] → (Option X))
(match lst with
[Nil -> None]
[Cons fst rst ->
(cond [(pred fst) (Some fst)]
[else (find rst pred)])]))
(check-type
(find (Cons 1 (Cons 2 (Cons 3 Nil))) (λ ([x : Int]) (<= 2 x)))
: (Option Int)
-> (Some 2))
(check-type
(find (Cons 1 (Cons 0 (Cons -1 Nil))) (λ ([x : Int]) (<= 2 x)))
: (Option Int)
-> (None {Int}))
(check-type
(find (Nil {Int}) (λ ([x : Int]) (<= 2 x)))
: (Option Int)
-> (None {Int}))

View File

@ -16,3 +16,4 @@
;; from rw ocaml
(require "mlish/term.mlish")
(require "mlish/find.mlish")