Skip to content

Instantly share code, notes, and snippets.

@keleshev
Last active April 3, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keleshev/52d1a47d31aea7eb96733a2c313054a8 to your computer and use it in GitHub Desktop.
Save keleshev/52d1a47d31aea7eb96733a2c313054a8 to your computer and use it in GitHub Desktop.
open Printf
let (=>) left right = printf "%c" (if left = right then '.' else 'F')
module CharString = struct
type t = Empty | Char of char * t
let rec fold ~empty ~char = function
| Empty -> empty
| Char (c, rest) -> char c (fold ~empty ~char rest)
let to_char_list =
fold ~empty:[] ~char:(fun char rest -> char :: rest)
end
module String = struct
type t = string
module Slice = struct
type t = {index: int; string: string}
let rec fold ~empty ~char {index; string} =
if index = String.length string then
empty
else
char (String.get string index)
(fold ~empty ~char {index=index + 1; string})
end
(*
let fold ~empty ~char string =
Slice.fold ~empty ~char Slice.{index=0; string}
*)
let fold ~empty ~char string =
let length = String.length string in
let rec go index =
if index = length then empty
else char (String.get string index) (go (index + 1))
in
go 0
let to_char_list =
fold ~empty:[] ~char:List.cons
module Test = struct
to_char_list "abcd" => ['a'; 'b'; 'c'; 'd']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment