; rest-of-value: ; integer * integer * list of bits -> number ; (rest-of-value base sofar lob) ; helper function for value-as-number ; assuming that the least significant bit in ; lob (part of an overall list of bits overall) ; has value base and that earlier bits in ; overall had value sofar, ; what is the total value for the overall number ; ; examples, relevant to the computation for value-as-number ; (rest-of-value 1 0 '(L L H H)) -> 12 ; (rest-of-value 2 0 '(L H H)) -> 12 ; (rest-of-value 4 0 '(H H)) -> 12 ; (rest-of-value 8 4 '(H)) -> 12 ; (rest-of-value 16 12 '()) -> 12 (define (rest-of-value base sofar lob) (if (null? lob) sofar (rest-of-value (* base 2) (+ sofar (if (eq? (car lob) 'H) base 0)) (cdr lob)))) ; value-as-number: ; list of bits -> number ; returns the integer value that list-of-bits would ; represent in base 2 with least significant bit first ; ; examples ; (value-as-number '(L L H H)) -> 12 ; (value-as-number '(H L L H H)) -> 25 (define (value-as-number lob) (rest-of-value 1 0 lob)) ; number-as-list ; number -> list-of-bits ; returns the binary representation of a whole number ; (least significant bit first) ; ; example ; (number-as-list 12) -> '(L L H H) ; (number-as-list 25) -> '(H L L H H) (define (number-as-list number) (if (zero? number) '() (cons (if (= (modulo number 2) 1) 'H 'L) (number-as-list (quotient number 2))))) ; add-one ; list-of-bits -> list-of-bits ; returns the binary representation of the ; integer represented by list-of-bits ; ; examples ; (add-one '(L L H H)) -> '(H L H H) ; (add-one '(H L H H)) -> '(L H H H) ; (add-one '(L H H H)) -> '(H H H H) ; (add-one '(H H H H)) -> '(L L L L H) (define (add-one lob) (cond [(null? lob) (list 'H)] [(eq? (car lob) 'L) (cons 'H (cdr lob))] [#t (cons 'L (add-one (cdr lob)))])) (define (subtract-one lob) (cond [(null? lob) (error "reached zero")] [(eq? (car lob) 'L) (cons 'H (subtract-one (cdr lob)))] [#t (cons 'L (cdr lob))])) ;----- ; Learning ;----- (print-struct #t) (define-struct datum (x y r) (make-inspector)) ; matches rule ex ; rule is a truth table ; ex is a (input-output) example ; ; return true if the output of rule ; on the inputs of ex ; is the same as the output of ex (define (matches? rule ex) (cond [(and (eq? (datum-x ex) 'L) (eq? (datum-y ex) 'L)) (eq? (list-ref rule 0) (datum-r ex))] [(and (eq? (datum-x ex) 'L) (eq? (datum-y ex) 'H)) (eq? (list-ref rule 1) (datum-r ex))] [(and (eq? (datum-x ex) 'H) (eq? (datum-y ex) 'L)) (eq? (list-ref rule 2) (datum-r ex))] [(and (eq? (datum-x ex) 'H) (eq? (datum-y ex) 'H)) (eq? (list-ref rule 3) (datum-r ex))])) ; matches-all? ; rule is a truth table ; data is a list of input-output examples ; ; return true if the output of rule ; matches each of the examples in data (define (matches-all? rule data) (or (null? data) (and (matches? rule (car data)) (matches-all? rule (cdr data))))) (define (next-matching-rule first-to-try data) (if (matches-all? first-to-try data) first-to-try (next-matching-rule (subtract-one first-to-try) data))) ; return the first truth table that's ; compatible with all the examples in data. ; error if data is inconsistent (define (first-matching-rule data) (next-matching-rule '(H H H H) data)) (define sample-data (list (make-datum 'H 'H 'H) (make-datum 'H 'L 'L) (make-datum 'L 'L 'L) (make-datum 'L 'H 'L))) (define bad-data (list (make-datum 'L 'L 'H) (make-datum 'L 'L 'L)))