(define-structure tape cells pos) (define (print-chars tape chars) (if (null? chars) "" (shortest (map (lambda (using-cell) (let* ((data (print-char tape (car chars) using-cell)) (new-tape (cdr data)) (code (car data))) (string-append code (print-chars new-tape (cdr chars))))) (from-to 0 (- (vector-length (tape-cells tape)) 1)))))) (define (print-char tape char using-cell) (cons (string-append (if (< using-cell (tape-pos tape)) (make-string (- (tape-pos tape) using-cell) #\<) (make-string (- using-cell (tape-pos tape)) #\>)) (if (< char (vector-ref (tape-cells tape) using-cell)) (make-string (- (vector-ref (tape-cells tape) using-cell) char) #\-) (make-string (- char (vector-ref (tape-cells tape) using-cell)) #\+)) ".") (make-tape (let ((v (vector-copy (tape-cells tape)))) (vector-set! v using-cell char) v) using-cell))) (define (from-to n m) (if (= n m) (list n) (cons n (from-to (+ n 1) m)))) (define (shortest lst) (let loop ((lst lst) (shortest #f) (len #f)) (cond ((null? lst) shortest) ((or (not shortest) (< (string-length (car lst)) len)) (loop (cdr lst) (car lst) (string-length (car lst)))) (else (loop (cdr lst) shortest len)))))