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 ;product : (listof numbers) -> numbers
5 12687dd9 2023-08-04 jrmu ;Computes the product of alon0.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu (define (product alon0)
8 12687dd9 2023-08-04 jrmu (local ;; the accumulator represents the product of all the numbers preceding alon1 in alon0,
9 12687dd9 2023-08-04 jrmu ;; ie, it represents the product of (alon1,alon0]
10 12687dd9 2023-08-04 jrmu ((define (product-accu alon1 accumulator)
11 12687dd9 2023-08-04 jrmu (cond
12 12687dd9 2023-08-04 jrmu [(empty? alon1) accumulator]
13 12687dd9 2023-08-04 jrmu [else (product-accu (rest alon1) (* (first alon1) accumulator))])))
14 12687dd9 2023-08-04 jrmu (product-accu alon0 1)))
15 12687dd9 2023-08-04 jrmu
16 12687dd9 2023-08-04 jrmu (equal? (product '(1 2 3 4 5 6)) 720)
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu ;how-many : (listof X) -> number
19 12687dd9 2023-08-04 jrmu ;Determines how many items there are in alox0.
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu (define (how-many alox0)
22 12687dd9 2023-08-04 jrmu (local ;;the accumulator represents the number of items preceding alox1 in alox0
23 12687dd9 2023-08-04 jrmu ((define (how-many-accu alox1 accumulator)
24 12687dd9 2023-08-04 jrmu (cond
25 12687dd9 2023-08-04 jrmu [(empty? alox1) accumulator]
26 12687dd9 2023-08-04 jrmu [else (how-many-accu (rest alox1) (+ 1 accumulator))])))
27 12687dd9 2023-08-04 jrmu (how-many-accu alox0 0)))
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu (equal? (how-many '(5 hey joe 5 1 0 true true false)) 9)
30 12687dd9 2023-08-04 jrmu
31 12687dd9 2023-08-04 jrmu ;add-to-pi : N -> number
32 12687dd9 2023-08-04 jrmu ;Adds pi to n.
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu (define (add-to-pi n0)
35 12687dd9 2023-08-04 jrmu (local ;; the accumulator represents n0-n1+pi.
36 12687dd9 2023-08-04 jrmu ((define (add-to-pi-accu n1 accumulator)
37 12687dd9 2023-08-04 jrmu (cond
38 12687dd9 2023-08-04 jrmu [(zero? n1) accumulator]
39 12687dd9 2023-08-04 jrmu [else (add-to-pi-accu (sub1 n1) (add1 accumulator))])))
40 12687dd9 2023-08-04 jrmu (add-to-pi-accu n0 pi)))
41 12687dd9 2023-08-04 jrmu
42 12687dd9 2023-08-04 jrmu ;add-to-x : N -> number
43 12687dd9 2023-08-04 jrmu ;Adds x to n.
44 12687dd9 2023-08-04 jrmu
45 12687dd9 2023-08-04 jrmu (define (add-to-x n0 x)
46 12687dd9 2023-08-04 jrmu (local ;; the accumulator represents n0-n1+x.
47 12687dd9 2023-08-04 jrmu ((define (add-to-x-accu n1 accumulator)
48 12687dd9 2023-08-04 jrmu (cond
49 12687dd9 2023-08-04 jrmu [(zero? n1) accumulator]
50 12687dd9 2023-08-04 jrmu [else (add-to-x-accu (sub1 n1) (add1 accumulator))])))
51 12687dd9 2023-08-04 jrmu (add-to-x-accu n0 x)))
52 12687dd9 2023-08-04 jrmu
53 12687dd9 2023-08-04 jrmu (equal? (add-to-x 5 6) 11)
54 12687dd9 2023-08-04 jrmu
55 12687dd9 2023-08-04 jrmu ;make-palindrome : (listof X) -> (listof X)
56 12687dd9 2023-08-04 jrmu ;Creates a palindrome from alox (non-empty list).
57 12687dd9 2023-08-04 jrmu
58 12687dd9 2023-08-04 jrmu (define (make-palindrome alox0)
59 12687dd9 2023-08-04 jrmu (local ;The accumulator represents the items in alox0 preceding alox1
60 12687dd9 2023-08-04 jrmu ;in reverse order as in alox0.
61 12687dd9 2023-08-04 jrmu ((define (make-palindrome-accu alox1 accumulator)
62 12687dd9 2023-08-04 jrmu (cond
63 12687dd9 2023-08-04 jrmu [(empty? alox1) empty]
64 12687dd9 2023-08-04 jrmu [(empty? (rest alox1)) (cons (first alox1) accumulator)]
65 12687dd9 2023-08-04 jrmu [else (cons (first alox1)
66 12687dd9 2023-08-04 jrmu (make-palindrome-accu (rest alox1) (cons (first alox1) accumulator)))])))
67 12687dd9 2023-08-04 jrmu (make-palindrome-accu alox0 empty)))
68 12687dd9 2023-08-04 jrmu
69 12687dd9 2023-08-04 jrmu (make-palindrome '(a b c 1 5 3))
70 12687dd9 2023-08-04 jrmu
71 12687dd9 2023-08-04 jrmu ;is-prime? : N -> boolean
72 12687dd9 2023-08-04 jrmu ;Determines if n is a prime number.
73 12687dd9 2023-08-04 jrmu
74 12687dd9 2023-08-04 jrmu (define (is-prime? n0)
75 12687dd9 2023-08-04 jrmu (local ;;accumulator i represents the numbers tested from [2,i) in order to
76 12687dd9 2023-08-04 jrmu ;;determine if n1 is prime
77 12687dd9 2023-08-04 jrmu ((define (is-prime?-accu n1 i)
78 12687dd9 2023-08-04 jrmu (cond
79 12687dd9 2023-08-04 jrmu [(= n1 i) true]
80 12687dd9 2023-08-04 jrmu [(= (remainder n1 i) 0) false]
81 12687dd9 2023-08-04 jrmu [else (is-prime?-accu n1 (add1 i))])))
82 12687dd9 2023-08-04 jrmu (is-prime?-accu n0 2)))
83 12687dd9 2023-08-04 jrmu
84 12687dd9 2023-08-04 jrmu (and (is-prime? 2)
85 12687dd9 2023-08-04 jrmu (is-prime? 3)
86 12687dd9 2023-08-04 jrmu (not (is-prime? 4))
87 12687dd9 2023-08-04 jrmu (is-prime? 5)
88 12687dd9 2023-08-04 jrmu (not (is-prime? 6))
89 12687dd9 2023-08-04 jrmu (is-prime? 7))