(********************************************************************)
(*  OCaml code from the book ``Learn Programming with OCaml''       *)
(*  See https://usr.lmf.cnrs.fr/lpo/                                *)
(*                                                                  *)
(*  Sylvain Conchon and Jean-Christophe Filliâtre                   *)
(*  Copyright 2025 Université Paris-Saclay and CNRS                 *)
(*                                                                  *)
(*  Openly licensed via CC BY SA 4.0                                *)
(*  See https://creativecommons.org/licenses/by-sa/4.0/deed.en      *)
(********************************************************************)

(* Program 27 on page 189
   Concatenation of Two Ropes *)

  let small_length = 256

  let append_string s1 ofs1 len1 s2 ofs2 len2 =
    Str (S.append (S.sub s1 ofs1 len1) (S.sub s2 ofs2 len2),
         0, len1 + len2)

  let append t1 t2 = match t1, t2 with
    | Str (_,_,0), t | t, Str (_,_,0) ->
        t
    | Str (s1, ofs1, len1), Str (s2, ofs2, len2)
     when len1 <= small_length && len2 <= small_length ->
        append_string s1 ofs1 len1 s2 ofs2 len2
    | App (t1, Str (s1, ofs1, len1), _), Str (s2, ofs2, len2)
     when len1 <= small_length && len2 <= small_length ->
        App (t1, append_string s1 ofs1 len1 s2 ofs2 len2,
             length t1 + len1 + len2)
    | Str (s1, ofs1, len1), App (Str (s2, ofs2, len2), t2, _)
     when len1 <= small_length && len2 <= small_length ->
        App (append_string s1 ofs1 len1 s2 ofs2 len2, t2,
             len1 + len2 + length t2)
    | t1, t2 ->
        App (t1, t2, length t1 + length t2)

  let (++) = append

This document was generated using caml2html