Skip to content

Instantly share code, notes, and snippets.

@vgrichina
Created February 12, 2016 08:59
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 vgrichina/a8e78752d8e625511374 to your computer and use it in GitHub Desktop.
Save vgrichina/a8e78752d8e625511374 to your computer and use it in GitHub Desktop.
data Rope = Empty | Concat Int Rope Rope | Leaf String deriving (Show, Eq)
rope :: String -> Rope
rope "" = Empty
rope s = Leaf s
len :: Rope -> Int
len Empty = 0
len (Leaf s) = length s
len (Concat length _ _) = length
asString :: Rope -> String
asString Empty = ""
asString (Leaf s) = s
asString (Concat _ left right) = (asString left) ++ (asString right)
cat :: Rope -> Rope -> Rope
Empty `cat` a = a
a `cat` Empty = a
a `cat` b = Concat ((len a) + (len b)) a b
rsplitAt :: Int -> Rope -> (Rope, Rope)
rsplitAt i (Leaf s) = (rope s1, rope s2) where (s1, s2) = (splitAt i s)
rsplitAt i (Concat _ left right)
| i < (len left) =
let (r1, r2) = (rsplitAt i left)
in (r1, r2 `cat` right)
| otherwise =
let (r1, r2) = (rsplitAt (i - (len left)) right)
in (left `cat` r1, r2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment