From d9940d81d01ad9f010e99c113ba3db0e3d52e272 Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Fri, 17 Dec 2021 07:23:03 +1100 Subject: [PATCH] tree map --- README.md | 2 +- lib/tree.ml | 9 +++++++-- lib/tree.mli | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4c81170..865755a 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,4 @@ Also take note of the fact that I typically compile everything with `-S` and `-O ## The Core Library -One of the unfortunate consequences of the way OCaml's compilation works, is that there is a library called the core library, documented [here](https://ocaml.org/manual/core.html), which contains some definitions for types and exceptions, yet does not include the code from the stdlib that uses them. When compiling with the `-nopervasives` flag, this is still included but without the standard library. While this makes sense from the perspective of having some fundamental exceptions always available, having types like `list` included makes it very annoying when implemented a custom standard library. This quirk is why my library has no type definition for `list`, `bool`, `option`, etc. but still uses these types. +One of the unfortunate consequences of the way OCaml's compilation works, is that there is a library called the core library, documented [here](https://ocaml.org/manual/core.html), which contains some definitions for types and exceptions, yet does not include the code from the stdlib that uses them. When compiling with the `-nopervasives` flag, this is still included but without the standard library. While this makes sense from the perspective of having some fundamental exceptions always available, having types like `list` included makes it very annoying when implemented a custom standard library. This quirk is why my library has no type definition for `list`, `bool`, `option`, etc. but still uses these types. \ No newline at end of file diff --git a/lib/tree.ml b/lib/tree.ml index aa47707..65c3718 100644 --- a/lib/tree.ml +++ b/lib/tree.ml @@ -1,5 +1,10 @@ -open List +open FromStdlib open Types let combine (tr1 : 'a tree) (tr2 : 'a tree) (topBranch : 'a) : 'a tree = - Branch (topBranch, tr1 :: tr2 :: []) \ No newline at end of file + Branch (topBranch, tr1 :: tr2 :: []) + +let rec map (f : 'a -> 'b) (tr : 'a tree) : 'b tree = + match tr with + | Branch (value, branches) -> Branch (f value, branches |> List.map (map f)) + | Leaf -> Leaf \ No newline at end of file diff --git a/lib/tree.mli b/lib/tree.mli index ec58ad4..2cb077c 100644 --- a/lib/tree.mli +++ b/lib/tree.mli @@ -1,4 +1,7 @@ open Types (* Combines two trees of the same type, with the specified value at the new top node. Runs in O(1). *) -val combine : 'a tree -> 'a tree -> 'a -> 'a tree \ No newline at end of file +val combine : 'a tree -> 'a tree -> 'a -> 'a tree + +(** Applies the provided function to each element of the tree. Does not use constant stack space. Runs in O(n). *) +val map : ('a -> 'b) -> 'a tree -> 'b tree \ No newline at end of file