Skip to content

Instantly share code, notes, and snippets.

@chuckis
Last active February 19, 2016 16:30
Show Gist options
  • Save chuckis/f8eebf2668b99e361429 to your computer and use it in GitHub Desktop.
Save chuckis/f8eebf2668b99e361429 to your computer and use it in GitHub Desktop.
knight tour in Lispme's scheme
;fnkon'
(define (filter f l)
(if (null? l) '()
(let ((x (car l))
(r (filter f (cdr l))))
(if (f x) (cons x r) r))))
(define (map f l)
(letrec ((result (cons '() '()))
(helper (lambda (p l)
(cond ((null? l) p)
(else (set-cdr! p
(cons (f (car l)) '()))
(helper (cdr p) (cdr l)))))))
(helper result l)
(cdr result)))
(define (make-coord x y)
(list x y))
(define (get-x coord)
(car coord))
(define (get-y coord)
(cadr coord))
(define (on-board? coord)
(if (and (and (> (get-x coord) 0)
(<= (get-x coord) 8))
(and (> (get-y coord) 0)
(<= (get-y coord) 8))) #t #f))
(define (neibrs coord)
(let ((x (get-x coord))
(y (get-y coord)))
(list
(make-coord (- x 2) (- y 1))
(make-coord (- x 1) (- y 2))
(make-coord (+ x 1) (- y 2))
(make-coord (+ x 2) (- y 1))
(make-coord (+ x 2) (+ y 1))
(make-coord (+ x 1) (+ y 2))
(make-coord (- x 1) (+ y 2))
(make-coord (- x 2) (+ y 1)))))
(define (eq-coords? c1 c2)
(if (and (= (get-x c1) (get-x c2))
(= (get-y c1) (get-y c2))) #t #f))
(define (coord-in-loc? coord loc)
;loc - List Of Coordinates
(cond
((null? loc) #f)
((eq-coords? coord (car loc)) #t)
(else (coord-in-loc? coord (cdr loc)))))
(define (n-o-b coord)
;this makes list of neibrs ON board
(let ((l-o-neib (neibrs coord)))
(filter on-board? l-o-neib)))
(define (k-m coord)
(define kml '())
(letrec ((kml '((1 1) (2 3)))
(lcand (n-o-b coord))
(loop (lambda (lcand) (if (coord-in-loc? (car lcand) kml) (loop (cdr lcand)) (set! kml (append kml (list (car lcand))))))))
(display (car (reverse kml)))
(set! kml (append kml (k-m (car (reverse kml)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment