Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-advanced-reader.ss" "lang")((modname |31.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 ;A digit is an N >=0 and <=9.
6 ;to10 : (listof digits) -> number
7 ;Converts alod1 into the corresponding number in base 10.
9 ;Examples:
10 ;(= (to10 (list 1 0 2))
11 ; 102)
12 ;
13 ;(= (to10 (list 2 1))
14 ; 21)
16 (define (to10 alod0)
17 (local ;; accu represents the digits preceding alod1 in alod0 that have been converted
18 ;; to base 10
19 ((define (to10-accu alod1 accu)
20 (cond
21 [(empty? alod1) accu]
22 [else (to10-accu (rest alod1) (+ (* accu 10)
23 (first alod1)))])))
24 (to10-accu alod0 0)))
26 (= (to10 (list 1 0 2 8 3 9 2 3))
27 10283923)
29 (= (to10 (list 2 1 9 8 7 2 3 4))
30 21987234)
32 ;A b-digit is an N >=0 and <=(b-1), where b is an N.
34 ;to-b : (listof b-digits) N -> number
35 ;Converts alod1 into the corresponding number in base b.
37 (define (to-b b alod0)
38 (local ;; accu represents the digits preceding alod1 in alod0 that have been converted
39 ;; to base 10
40 ((define (to-b-accu alod1 accu)
41 (cond
42 [(empty? alod1) accu]
43 [else (to-b-accu (rest alod1) (+ (* accu b)
44 (first alod1)))])))
45 (to-b-accu alod0 0)))
47 (equal? (to-b 8 '(1 0 2)) 66)
48 (equal? (to-b 10 '(1 0 2)) 102)
49 (equal? (to-b 16 '(1 0 2 6 4 3)) 1058371)