[ create a new paste ] login | about

Link: http://codepad.org/6XlJrVvZ    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jun 23:
; busy beaver

(define (make-hash hash eql? oops size)
  (let ((table (make-vector size '())))
    (lambda (message . args)
      (if (eq? message 'enlist)
          (let loop ((k 0) (result '()))
            (if (= size k)
                result
                (loop (+ k 1) (append (vector-ref table k) result))))
          (let* ((key (car args))
                 (index (modulo (hash key) size))
                 (bucket (vector-ref table index)))
            (case message
              ((lookup fetch get ref recall)
                (let loop ((bucket bucket))
                  (cond ((null? bucket) oops)
                        ((eql? (caar bucket) key) (cdar bucket))
                        (else (loop (cdr bucket))))))
              ((insert insert! ins ins! set set! store store! install install!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket)
                            (list (cons key (cadr args))))
                          ((eql? (caar bucket) key)
                            (cons (cons key (cadr args)) (cdr bucket)))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              ((delete delete! del del! remove remove!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket) '())
                          ((eql? (caar bucket) key)
                            (cdr bucket))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              ((update update!)
                (vector-set! table index
                  (let loop ((bucket bucket))
                    (cond ((null? bucket)
                            (list (cons key (caddr args))))
                          ((eql? (caar bucket) key)
                            (cons (cons key ((cadr args) key (cdar bucket))) (cdr bucket)))
                          (else (cons (car bucket) (loop (cdr bucket))))))))
              (else (error 'hash-table "unrecognized message")) ))))))

(define (take n xs)
  (let loop ((n n) (xs xs) (ys '()))
    (if (or (zero? n) (null? xs))
        (reverse ys)
        (loop (- n 1) (cdr xs)
              (cons (car xs) ys)))))

(define (drop n xs)
  (let loop ((n n) (xs xs))
    (if (or (zero? n) (null? xs)) xs
      (loop (- n 1) (cdr xs)))))

(define blanks
  (let ((x (list #\space)))
    (set-cdr! x x)
    (cons x x)))

(define (read-cell tape) (cadr tape))

(define (write-cell chr tape)
  (cons (car tape) (cons chr (cddr tape))))

(define (move-left tape)
  (cons (cdar tape) (cons (caar tape) (cdr tape))))

(define (move-right tape)
  (cons (cons (cadr tape) (car tape)) (cddr tape)))

(define (make-tape chrs curr)
  (define (insert-cell chr tape)
    (cons (car tape) (cons chr (cdr tape))))
  (let loop ((curr curr) (chrs (reverse (string->list chrs))) (tape blanks))
    (cond ((= curr -1) (move-left tape))
          ((null? chrs) (loop (- curr 1) chrs (move-right tape)))
          (else (loop curr (cdr chrs) (insert-cell (car chrs) tape))))))

(define (show-tape tape)
  (let loop ((xs (car tape)) (k 0) (zs (list)))
    (if (= k 10) (display (drop 10 zs))
      (if (char=? (car xs) #\space)
          (loop (cdr xs) (+ k 1) (cons (car xs) zs))
          (loop (cdr xs) 0 (cons (car xs) zs)))))
  (display (cadr tape))
  (let loop ((xs (cddr tape)) (k 0) (zs (list)))
    (if (= k 10) (display (reverse (drop 10 zs)))
      (if (char=? (car xs) #\space)
          (loop (cdr xs) (+ k 1) (cons (car xs) zs))
          (loop (cdr xs) 0 (cons (car xs) zs)))))
  (newline))

(define (hash-state-symbol key)
  (+ (* (car key) 256) (char->integer (cadr key))))

(define (make-prog tuples)
  (let ((prog (make-hash hash-state-symbol equal? #f 97)))
    (do ((tuples tuples (cdr tuples)))
        ((null? tuples) prog)
      (prog 'insert (take 2 (car tuples)) (drop 2 (car tuples))))))

(define (show-beaver k state cmd tape)
  (display k) (display " ") (display state) (display " ")
  (display cmd) (display " ") (show-tape tape))

(define (turing prog tape)
  (let loop ((k 0) (state 0) (tape tape))
    (if (negative? state) (show-beaver k state "final" tape)
      (let ((cmd (prog 'lookup (list state (read-cell tape)))))
        (show-beaver k state cmd tape)
        (loop (+ k 1) (caddr cmd) (case (cadr cmd)
          ((left) (move-left (write-cell (car cmd) tape)))
          ((right) (move-right (write-cell (car cmd) tape)))
          (else (write-cell (car cmd) tape))))))))

(define (beaver bb) (turing (make-prog bb) (make-tape "" 0)))

(define bb1 '(
  (0 #\space #\*     right -1)))

(define bb2 '(
  (0 #\space #\*     right  1)
  (0 #\*     #\*     left   1)
  (1 #\space #\*     left   0)
  (1 #\*     #\*     right -1)))

(define bb3 '(
  (0 #\space #\*     right  1)
  (0 #\*     #\*     right -1)
  (1 #\space #\space right  2)
  (1 #\*     #\*     right  1)
  (2 #\space #\*     left   2)
  (2 #\*     #\*     left   0)))

(define bb4 '(
  (0 #\space #\*     right  1)
  (0 #\*     #\*     left   1)
  (1 #\space #\*     left   0)
  (1 #\*     #\space left   2)
  (2 #\space #\*     right -1)
  (2 #\*     #\*     left   3)
  (3 #\space #\*     right  3)
  (3 #\*     #\space right  0)))

(beaver bb1) (newline)
(beaver bb2) (newline)
(beaver bb3) (newline)
(beaver bb4)


Output:
0 0 (* right -1) () ()
1 -1 final (*) ()

0 0 (* right 1) () ()
1 1 (* left 0) (*) ()
2 0 (* left 1) ()*(*)
3 1 (* left 0) () (* *)
4 0 (* right 1) () (* * *)
5 1 (* right -1) (*)*(* *)
6 -1 final (* *)*(*)

0 0 (* right 1) () ()
1 1 (  right 2) (*) ()
2 2 (* left 2) (*  ) ()
3 2 (* left 2) (*) (*)
4 2 (* left 0) ()*(* *)
5 0 (* right 1) () (* * *)
6 1 (* right 1) (*)*(* *)
7 1 (* right 1) (* *)*(*)
8 1 (* right 1) (* * *)*()
9 1 (  right 2) (* * * *) ()
10 2 (* left 2) (* * * *  ) ()
11 2 (* left 2) (* * * *) (*)
12 2 (* left 0) (* * *)*(* *)
13 0 (* right -1) (* *)*(* * *)
14 -1 final (* * *)*(* *)

0 0 (* right 1) () ()
1 1 (* left 0) (*) ()
2 0 (* left 1) ()*(*)
3 1 (* left 0) () (* *)
4 0 (* right 1) () (* * *)
5 1 (  left 2) (*)*(* *)
6 2 (* left 3) ()*(  * *)
7 3 (* right 3) () (*   * *)
8 3 (  right 0) (*)*(  * *)
9 0 (* right 1) (*  ) (* *)
10 1 (  left 2) (*   *)*(*)
11 2 (* left 3) (*  )*(  *)
12 3 (* right 3) (*) (*   *)
13 3 (  right 0) (* *)*(  *)
14 0 (* right 1) (* *  ) (*)
15 1 (  left 2) (* *   *)*()
16 2 (* left 3) (* *  )*()
17 3 (* right 3) (* *) (*)
18 3 (  right 0) (* * *)*()
19 0 (* right 1) (* * *  ) ()
20 1 (* left 0) (* * *   *) ()
21 0 (* left 1) (* * *  )*(*)
22 1 (* left 0) (* * *) (* *)
23 0 (* left 1) (* *)*(* * *)
24 1 (  left 2) (*)*(* * * *)
25 2 (* left 3) ()*(  * * * *)
26 3 (* right 3) () (*   * * * *)
27 3 (  right 0) (*)*(  * * * *)
28 0 (* right 1) (*  ) (* * * *)
29 1 (  left 2) (*   *)*(* * *)
30 2 (* left 3) (*  )*(  * * *)
31 3 (* right 3) (*) (*   * * *)
32 3 (  right 0) (* *)*(  * * *)
33 0 (* right 1) (* *  ) (* * *)
34 1 (  left 2) (* *   *)*(* *)
35 2 (* left 3) (* *  )*(  * *)
36 3 (* right 3) (* *) (*   * *)
37 3 (  right 0) (* * *)*(  * *)
38 0 (* right 1) (* * *  ) (* *)
39 1 (  left 2) (* * *   *)*(*)
40 2 (* left 3) (* * *  )*(  *)
41 3 (* right 3) (* * *) (*   *)
42 3 (  right 0) (* * * *)*(  *)
43 0 (* right 1) (* * * *  ) (*)
44 1 (  left 2) (* * * *   *)*()
45 2 (* left 3) (* * * *  )*()
46 3 (* right 3) (* * * *) (*)
47 3 (  right 0) (* * * * *)*()
48 0 (* right 1) (* * * * *  ) ()
49 1 (* left 0) (* * * * *   *) ()
50 0 (* left 1) (* * * * *  )*(*)
51 1 (* left 0) (* * * * *) (* *)
52 0 (* left 1) (* * * *)*(* * *)
53 1 (  left 2) (* * *)*(* * * *)
54 2 (* left 3) (* *)*(  * * * *)
55 3 (  right 0) (*)*(*   * * * *)
56 0 (* left 1) (*  )*(  * * * *)
57 1 (* left 0) (*) (*   * * * *)
58 0 (* left 1) ()*(* *   * * * *)
59 1 (* left 0) () (* * *   * * * *)
60 0 (* right 1) () (* * * *   * * * *)
61 1 (  left 2) (*)*(* * *   * * * *)
62 2 (* left 3) ()*(  * * *   * * * *)
63 3 (* right 3) () (*   * * *   * * * *)
64 3 (  right 0) (*)*(  * * *   * * * *)
65 0 (* right 1) (*  ) (* * *   * * * *)
66 1 (  left 2) (*   *)*(* *   * * * *)
67 2 (* left 3) (*  )*(  * *   * * * *)
68 3 (* right 3) (*) (*   * *   * * * *)
69 3 (  right 0) (* *)*(  * *   * * * *)
70 0 (* right 1) (* *  ) (* *   * * * *)
71 1 (  left 2) (* *   *)*(*   * * * *)
72 2 (* left 3) (* *  )*(  *   * * * *)
73 3 (* right 3) (* *) (*   *   * * * *)
74 3 (  right 0) (* * *)*(  *   * * * *)
75 0 (* right 1) (* * *  ) (*   * * * *)
76 1 (  left 2) (* * *   *)*(  * * * *)
77 2 (* left 3) (* * *  )*(    * * * *)
78 3 (* right 3) (* * *) (*     * * * *)
79 3 (  right 0) (* * * *)*(    * * * *)
80 0 (* right 1) (* * * *  ) (  * * * *)
81 1 (* left 0) (* * * *   *) (* * * *)
82 0 (* left 1) (* * * *  )*(* * * * *)
83 1 (* left 0) (* * * *) (* * * * * *)
84 0 (* left 1) (* * *)*(* * * * * * *)
85 1 (  left 2) (* *)*(* * * * * * * *)
86 2 (* left 3) (*)*(  * * * * * * * *)
87 3 (  right 0) ()*(*   * * * * * * * *)
88 0 (* left 1) ()*(  * * * * * * * *)
89 1 (* left 0) () (*   * * * * * * * *)
90 0 (* right 1) () (* *   * * * * * * * *)
91 1 (  left 2) (*)*(*   * * * * * * * *)
92 2 (* left 3) ()*(  *   * * * * * * * *)
93 3 (* right 3) () (*   *   * * * * * * * *)
94 3 (  right 0) (*)*(  *   * * * * * * * *)
95 0 (* right 1) (*  ) (*   * * * * * * * *)
96 1 (  left 2) (*   *)*(  * * * * * * * *)
97 2 (* left 3) (*  )*(    * * * * * * * *)
98 3 (* right 3) (*) (*     * * * * * * * *)
99 3 (  right 0) (* *)*(    * * * * * * * *)
100 0 (* right 1) (* *  ) (  * * * * * * * *)
101 1 (* left 0) (* *   *) (* * * * * * * *)
102 0 (* left 1) (* *  )*(* * * * * * * * *)
103 1 (* left 0) (* *) (* * * * * * * * * *)
104 0 (* left 1) (*)*(* * * * * * * * * * *)
105 1 (  left 2) ()*(* * * * * * * * * * * *)
106 2 (* right -1) () (  * * * * * * * * * * * *)
107 -1 final (*) (* * * * * * * * * * * *)


Create a new paste based on this one


Comments: