Created
August 31, 2018 12:02
-
-
Save emillon/e1a1b660c5308f2bfeffbad6840b456b to your computer and use it in GitHub Desktop.
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
(* Step 1: 'a list -> bool everywhere. *) | |
(* Step 2: Wrap in a newtype: Finder of 'a list -> bool *) | |
(* Step 3: abstract in a module with this signature *) | |
module type LIST_FINDER = sig | |
type 'a t | |
val this_element : 'a -> 'a t | |
val find : 'a t -> 'a list -> bool | |
end | |
(* And this implementation *) | |
module Before : LIST_FINDER = struct | |
type 'a t = Finder of ('a list -> bool) | |
let this_element x = Finder (List.mem x) | |
let find (Finder f) l = f l | |
end | |
(* Step 4: invert constructor and deconstructor *) | |
module After : LIST_FINDER = struct | |
type 'a t = 'a | |
let this_element x = x | |
let find x l = List.mem x l | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment