Last active
April 3, 2018 12:48
-
-
Save keleshev/52d1a47d31aea7eb96733a2c313054a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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