Created
May 5, 2010 14:11
-
-
Save anonymous/390820 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
// Learn more about F# at http://fsharp.net | |
#light | |
module Module1 | |
open System | |
type Rope = Rope of int * int * RopeNode | |
and RopeNode = | |
Concat of Rope * Rope | |
| Leaf of String | |
// | Lazy of (Unit -> Rope) | |
let depth = fun (Rope(depth,length,content)) -> depth | |
let length = fun (Rope(depth,length,content)) -> length | |
let concat a b = Rope(max (depth a) (depth b), | |
length a + length b, | |
Concat(a,b)) | |
let rec subrope start offset rope = | |
let subrope_ x = match x with | |
| Concat(rope1, rope2) -> | |
let left = | |
if (start <= 0 && offset >= length rope1) then | |
rope1 | |
else subrope start offset rope | |
let right = | |
if (start <= length rope1 && start + offset >= length rope1 + length rope2) then | |
rope2 | |
else subrope (start - length rope1) (offset - length left) rope | |
concat left right | |
| Leaf(str) -> | |
let substring = str.Substring(start, offset) | |
Rope(1,offset,Leaf(substring)) | |
match rope with Rope(d, l, node) -> subrope_ node | |
let rec fromString (str : String) = | |
if str.Length < 20 then | |
Rope(1, str.Length, Leaf(str)) | |
else | |
let left = str.Substring(0, str.Length/2) | |
let right = str.Substring(str.Length/2) | |
concat (fromString left) (fromString right) | |
let rec toStrings = function | |
| Rope(d,l,Leaf(str)) -> [str] | |
| Rope(d,l,Concat(r1, r2)) -> append (toStrings r1) (toStrings r2) | |
type RopeIterator = class | |
interface Enumerable with | |
member x. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment