Skip to content

Instantly share code, notes, and snippets.

@matoous
Created June 6, 2018 16:42
Show Gist options
  • Save matoous/9e5422e8c357e2ff6b976ebf5bf9bb2a to your computer and use it in GitHub Desktop.
Save matoous/9e5422e8c357e2ff6b976ebf5bf9bb2a to your computer and use it in GitHub Desktop.
#lang scheme
(define new-list '(() ()))
(define (left l) (car l))
(define (right l) (car (cdr l)))
(define (insert x l)
(list (left l) (cons x (right l))))
(define (delete l)
(list (left l) (cdr (right l))))
(define (move-right l)
(list (cons (car (right l)) (left l)) (cdr (right l))))
(define (move-left l)
(list (cdr (left l)) (cons (car (left l)) (right l))))
(define (update x l)
(list (left l) (cons x (cdr (right l)))))
(define (get l)
(car (right l)))
(define (rewind x)
(if (eq? (left x) '()) x
(rewind (list (cdr (left x)) (cons (car (left x)) (right x))))))
(define (index x)
(length (left x)))
(define (move-to x l)
(if (> x (index l)) (move-to x (move-right l))
(if (= x (index l)) l
(move-to x (move-left l)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment