2019-08-09 10:18:36 -04:00
|
|
|
; dictionaries ---------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (dict-new) ())
|
|
|
|
|
|
|
|
(define (dict-extend dl key value)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((null? dl) (list (cons key value)))
|
|
|
|
((equal? key (caar dl)) (cons (cons key value) (cdr dl)))
|
|
|
|
(else (cons (car dl) (dict-extend (cdr dl) key value)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (dict-lookup dl key)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((null? dl) ())
|
|
|
|
((equal? key (caar dl)) (cdar dl))
|
|
|
|
(else (dict-lookup (cdr dl) key))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (dict-keys dl) (map car dl))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; graphs ---------------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (graph-empty) (dict-new))
|
|
|
|
|
|
|
|
(define (graph-connect g n1 n2)
|
|
|
|
(dict-extend
|
|
|
|
(dict-extend g n2 (cons n1 (dict-lookup g n2)))
|
|
|
|
n1
|
|
|
|
(cons n2 (dict-lookup g n1))))
|
|
|
|
|
|
|
|
(define (graph-adjacent? g n1 n2) (member n2 (dict-lookup g n1)))
|
|
|
|
|
|
|
|
(define (graph-neighbors g n) (dict-lookup g n))
|
|
|
|
|
|
|
|
(define (graph-nodes g) (dict-keys g))
|
|
|
|
|
|
|
|
(define (graph-add-node g n1) (dict-extend g n1 ()))
|
|
|
|
|
|
|
|
(define (graph-from-edges edge-list)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? edge-list)
|
2008-06-30 21:54:22 -04:00
|
|
|
(graph-empty)
|
|
|
|
(graph-connect (graph-from-edges (cdr edge-list))
|
|
|
|
(caar edge-list)
|
|
|
|
(cdar edge-list))))
|
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; graph coloring -------------------------------------------------------------
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (node-colorable? g coloring node-to-color color-of-node)
|
|
|
|
(not (member
|
|
|
|
color-of-node
|
|
|
|
(map
|
|
|
|
(lambda (n)
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(let ((color-pair (assq n coloring)))
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (pair? color-pair) (cdr color-pair) ())))
|
2008-06-30 21:54:22 -04:00
|
|
|
(graph-neighbors g node-to-color)))))
|
|
|
|
|
|
|
|
(define (try-each f lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? lst) #f
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(let ((ret (f (car lst))))
|
2019-08-09 10:18:36 -04:00
|
|
|
(if ret ret (try-each f (cdr lst))))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (color-node g coloring colors uncolored-nodes color)
|
|
|
|
(cond
|
2009-01-31 20:53:58 -05:00
|
|
|
((null? uncolored-nodes) coloring)
|
2008-06-30 21:54:22 -04:00
|
|
|
((node-colorable? g coloring (car uncolored-nodes) color)
|
|
|
|
(let ((new-coloring
|
|
|
|
(cons (cons (car uncolored-nodes) color) coloring)))
|
|
|
|
(try-each (lambda (c)
|
|
|
|
(color-node g new-coloring colors (cdr uncolored-nodes) c))
|
|
|
|
colors)))))
|
|
|
|
|
|
|
|
(define (color-graph g colors)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? colors)
|
|
|
|
(and (null? (graph-nodes g)) ())
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(color-node g () colors (graph-nodes g) (car colors))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (color-pairs pairs colors)
|
|
|
|
(color-graph (graph-from-edges pairs) colors))
|
|
|
|
|
2019-08-09 10:18:36 -04:00
|
|
|
; queens ---------------------------------------------------------------------
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (can-attack x y)
|
2008-06-30 21:54:22 -04:00
|
|
|
(let ((x1 (mod x 5))
|
|
|
|
(y1 (truncate (/ x 5)))
|
|
|
|
(x2 (mod y 5))
|
|
|
|
(y2 (truncate (/ y 5))))
|
|
|
|
(or (= x1 x2) (= y1 y2) (= (abs (- y2 y1)) (abs (- x2 x1))))))
|
|
|
|
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(define (generate-5x5-pairs)
|
|
|
|
(let ((result ()))
|
2008-06-30 21:54:22 -04:00
|
|
|
(dotimes (x 25)
|
|
|
|
(dotimes (y 25)
|
2009-08-09 14:04:03 -04:00
|
|
|
(if (and (not (= x y)) (can-attack x y))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(set! result (cons (cons x y) result)) ())))
|
2008-06-30 21:54:22 -04:00
|
|
|
result))
|