tree map
This commit is contained in:
parent
bf2cae7c4e
commit
d9940d81d0
@ -44,4 +44,4 @@ Also take note of the fact that I typically compile everything with `-S` and `-O
|
|||||||
|
|
||||||
## The Core Library
|
## 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.
|
@ -1,5 +1,10 @@
|
|||||||
open List
|
open FromStdlib
|
||||||
open Types
|
open Types
|
||||||
|
|
||||||
let combine (tr1 : 'a tree) (tr2 : 'a tree) (topBranch : 'a) : 'a tree =
|
let combine (tr1 : 'a tree) (tr2 : 'a tree) (topBranch : 'a) : 'a tree =
|
||||||
Branch (topBranch, tr1 :: tr2 :: [])
|
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
|
@ -1,4 +1,7 @@
|
|||||||
open Types
|
open Types
|
||||||
|
|
||||||
(* Combines two trees of the same type, with the specified value at the new top node. Runs in O(1). *)
|
(* 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
|
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
|
Loading…
Reference in New Issue
Block a user