Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Last active December 2, 2020 22:51
Show Gist options
  • Save ebresafegaga/e1c5ddb6943e8740317b0ba583c95f19 to your computer and use it in GitHub Desktop.
Save ebresafegaga/e1c5ddb6943e8740317b0ba583c95f19 to your computer and use it in GitHub Desktop.
ZipList in OCaml with Jane Street's Core
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