package sexp

  1. Overview
  2. Docs
S-expression swiss knife

Install

Dune Dependency

Authors

Maintainers

Sources

sexp-v0.16.0.tar.gz
sha256=bde6acfd2814bcc38a0d3cacb42e513d8932595152dd9798419559fb0e026f4e

doc/src/sexp.sexp_app/string_pad.ml.html

Source file string_pad.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
open Core

(*
   Creating a pad and adding a bunch of strings to it has asymtotic time
   and space complexity O(n) where n is the total number of characters.
   Every once and a while, there will be a very expensive [add] operation.
*)

(** INVARIANT: strings are in order of decreasing length from right to left *)
type t =
  | Empty
  | Snoc of t * string

let empty = Empty

let rec add t x =
  match t with
  | Empty -> Snoc (Empty, x)
  | Snoc (rest, y) ->
    if String.length y > String.length x then Snoc (t, x) else add rest (y ^ x)
;;

let singleton x = add empty x
let add_char t c = add t (String.of_char c)

let rec dump = function
  | Empty -> ""
  | Snoc (Empty, x) -> x
  | Snoc (Snoc (t, x), y) ->
    dump (Snoc (t, x ^ y))
;;
OCaml

Innovation. Community. Security.