Created
December 5, 2012 20:04
-
-
Save lfranchi/4219014 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
(defn zipper | |
[] | |
[() ()]) | |
(defn insert-at-cursor | |
[zipper newchar] | |
[(cons newchar (first zipper)) (second zipper)]) | |
(defn get-at-cursor | |
[zipper] | |
(ffirst zipper)) | |
(defn move-left | |
[zipper] | |
[(rest (first zipper)) (cons (ffirst zipper) (second zipper))]) | |
(defn move-right | |
[zipper] | |
[(cons (first (second zipper)) (first zipper)) (rest (second zipper))]) | |
(defn delete-at-cursor | |
[zipper] | |
[(rest (first zipper)) (second zipper)]) | |
;; tests | |
(def z (zipper)) | |
(def z (insert-at-cursor z "a")) | |
(def z (insert-at-cursor z "b")) | |
(def z (insert-at-cursor z "c")) | |
(def z (insert-at-cursor z "d")) | |
(get-at-cursor z) | |
(def z (move-left z)) | |
(get-at-cursor z) | |
(def z (delete-at-cursor z)) | |
(get-at-cursor z) | |
(def z (insert-at-cursor z "q")) | |
(get-at-cursor z) | |
(def z (move-right z)) | |
(get-at-cursor z) | |
(def z (move-right z)) | |
(get-at-cursor z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment