Last active
December 2, 2020 22:51
-
-
Save ebresafegaga/e1c5ddb6943e8740317b0ba583c95f19 to your computer and use it in GitHub Desktop.
ZipList in OCaml with Jane Street's Core
This file contains hidden or 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
open Core | |
module ZipList = struct | |
type 'a t = ZipList of 'a list | |
let return x = | |
(* A nice feature of Ocaml; just make sure the operation performed on the list | |
doesn't "recur" over the whole list. It has to be something like `take`, or a | |
`zip` with another finite list *) | |
let rec l = x :: l in | |
ZipList l | |
let map = `Define_using_apply | |
let rec apply fs xs = | |
match fs, xs with | |
| ZipList (f :: fs), ZipList (x :: xs) -> | |
let ZipList almost = apply (ZipList fs) (ZipList xs) in | |
ZipList (f x :: almost) | |
| ZipList [], _ | _, ZipList [] -> ZipList [] | |
end | |
let test () = | |
let open Applicative.Make (ZipList) in | |
ZipList [ (+); (-) ] <*> ZipList [ 1;2 ] <*> ZipList [ 2;3 ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment