Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #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 12687dd9 2023-08-04 jrmu ;A digit is an N >=0 and <=9.
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;to10 : (listof digits) -> number
7 12687dd9 2023-08-04 jrmu ;Converts alod1 into the corresponding number in base 10.
8 12687dd9 2023-08-04 jrmu
9 12687dd9 2023-08-04 jrmu ;Examples:
10 12687dd9 2023-08-04 jrmu ;(= (to10 (list 1 0 2))
11 12687dd9 2023-08-04 jrmu ; 102)
12 12687dd9 2023-08-04 jrmu ;
13 12687dd9 2023-08-04 jrmu ;(= (to10 (list 2 1))
14 12687dd9 2023-08-04 jrmu ; 21)
15 12687dd9 2023-08-04 jrmu
16 12687dd9 2023-08-04 jrmu (define (to10 alod0)
17 12687dd9 2023-08-04 jrmu (local ;; accu represents the digits preceding alod1 in alod0 that have been converted
18 12687dd9 2023-08-04 jrmu ;; to base 10
19 12687dd9 2023-08-04 jrmu ((define (to10-accu alod1 accu)
20 12687dd9 2023-08-04 jrmu (cond
21 12687dd9 2023-08-04 jrmu [(empty? alod1) accu]
22 12687dd9 2023-08-04 jrmu [else (to10-accu (rest alod1) (+ (* accu 10)
23 12687dd9 2023-08-04 jrmu (first alod1)))])))
24 12687dd9 2023-08-04 jrmu (to10-accu alod0 0)))
25 12687dd9 2023-08-04 jrmu
26 12687dd9 2023-08-04 jrmu (= (to10 (list 1 0 2 8 3 9 2 3))
27 12687dd9 2023-08-04 jrmu 10283923)
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu (= (to10 (list 2 1 9 8 7 2 3 4))
30 12687dd9 2023-08-04 jrmu 21987234)
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu ;A b-digit is an N >=0 and <=(b-1), where b is an N.
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu ;to-b : (listof b-digits) N -> number
35 12687dd9 2023-08-04 jrmu ;Converts alod1 into the corresponding number in base b.
36 12687dd9 2023-08-04 jrmu
37 12687dd9 2023-08-04 jrmu (define (to-b b alod0)
38 12687dd9 2023-08-04 jrmu (local ;; accu represents the digits preceding alod1 in alod0 that have been converted
39 12687dd9 2023-08-04 jrmu ;; to base 10
40 12687dd9 2023-08-04 jrmu ((define (to-b-accu alod1 accu)
41 12687dd9 2023-08-04 jrmu (cond
42 12687dd9 2023-08-04 jrmu [(empty? alod1) accu]
43 12687dd9 2023-08-04 jrmu [else (to-b-accu (rest alod1) (+ (* accu b)
44 12687dd9 2023-08-04 jrmu (first alod1)))])))
45 12687dd9 2023-08-04 jrmu (to-b-accu alod0 0)))
46 12687dd9 2023-08-04 jrmu
47 12687dd9 2023-08-04 jrmu (equal? (to-b 8 '(1 0 2)) 66)
48 12687dd9 2023-08-04 jrmu (equal? (to-b 10 '(1 0 2)) 102)
49 12687dd9 2023-08-04 jrmu (equal? (to-b 16 '(1 0 2 6 4 3)) 1058371)