Created
August 12, 2012 19:39
-
-
Save owainlewis/3334034 to your computer and use it in GitHub Desktop.
OCaml Functors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type comparison = Less | Equal | Greater | |
module type ORDERED_TYPE = | |
sig | |
type t | |
val compare: t -> t -> comparison | |
end;; | |
module type ORDERED_TYPE = sig type t val compare : t -> t -> comparison end | |
module Set = | |
functor (Elt: ORDERED_TYPE) -> | |
struct | |
type element = Elt.t | |
type set = element list | |
let empty = [] | |
let rec add x s = | |
match s with | |
[] -> [x] | |
| hd::tl -> | |
match Elt.compare x hd with | |
Equal -> s (* x is already in s *) | |
| Less -> x :: s (* x is smaller than all elements of s *) | |
| Greater -> hd :: add x tl | |
let rec member x s = | |
match s with | |
[] -> false | |
| hd::tl -> | |
match Elt.compare x hd with | |
Equal -> true (* x belongs to s *) | |
| Less -> false (* x is smaller than all elements of s *) | |
| Greater -> member x tl | |
end;; | |
module Set : | |
functor (Elt : ORDERED_TYPE) -> | |
sig | |
type element = Elt.t | |
type set = element list | |
val empty : 'a list | |
val add : Elt.t -> Elt.t list -> Elt.t list | |
val member : Elt.t -> Elt.t list -> bool | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment