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-intermediate-reader.ss" "lang")((modname |23.2|) (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 #f #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-fives : N[>=0] -> number
5 12687dd9 2023-08-04 jrmu ;Given n, determine recursively the value of the n-th term in the series 8, 13, 18, etc. where the first term has an index 0.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu (define (a-fives n)
8 12687dd9 2023-08-04 jrmu (cond
9 12687dd9 2023-08-04 jrmu [(zero? n) 8]
10 12687dd9 2023-08-04 jrmu [else (+ 5 (a-fives (sub1 n)))]))
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;a-fives-closed : N[>=0] -> number
13 12687dd9 2023-08-04 jrmu ;A non-recursive function for finding the n-th term of the series 8, 13, 18, etc., where the first term has an index 0.
14 12687dd9 2023-08-04 jrmu
15 12687dd9 2023-08-04 jrmu (define (a-fives-closed n)
16 12687dd9 2023-08-04 jrmu (+ (* n 5) 8))
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu ;series : (N[>=0] -> number) N[>=0] -> number
19 12687dd9 2023-08-04 jrmu ;Given a-term and n, determine the sum of the first n-th terms in the sequence a-term.
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu (define (series a-term n)
22 12687dd9 2023-08-04 jrmu (cond
23 12687dd9 2023-08-04 jrmu [(zero? n) (a-term n)]
24 12687dd9 2023-08-04 jrmu [else (+ (a-term n)
25 12687dd9 2023-08-04 jrmu (series a-term (sub1 n)))]))
26 12687dd9 2023-08-04 jrmu
27 12687dd9 2023-08-04 jrmu ;sequence : (N -> number) N -> (listof number)
28 12687dd9 2023-08-04 jrmu ;Given a-term and n, generate the sequence of the first nth terms in a-term as a (listof numbers).
29 12687dd9 2023-08-04 jrmu
30 12687dd9 2023-08-04 jrmu (define (sequence a-term n)
31 12687dd9 2023-08-04 jrmu (build-list n a-term))
32 12687dd9 2023-08-04 jrmu
33 12687dd9 2023-08-04 jrmu (define (seq-a-fives n)
34 12687dd9 2023-08-04 jrmu (sequence a-fives-closed n))
35 12687dd9 2023-08-04 jrmu
36 12687dd9 2023-08-04 jrmu ;arithmetic-term : N number -> (N -> number)
37 12687dd9 2023-08-04 jrmu ;Given start and s (summand), return a function that consumes a natural number n and returns the nth-term in the arithmetic sequence.
38 12687dd9 2023-08-04 jrmu (define (arithmetic-term start s)
39 12687dd9 2023-08-04 jrmu (local ((define (a-term n)
40 12687dd9 2023-08-04 jrmu (+ (* n s) start)))
41 12687dd9 2023-08-04 jrmu a-term))
42 12687dd9 2023-08-04 jrmu
43 12687dd9 2023-08-04 jrmu (define a-fives2 (arithmetic-term 8 5))
44 12687dd9 2023-08-04 jrmu (define evens (arithmetic-term 0 2))
45 12687dd9 2023-08-04 jrmu
46 12687dd9 2023-08-04 jrmu ;g-fives : N -> number
47 12687dd9 2023-08-04 jrmu ;Recursively returns the nth term of the geometric sequence: 3, 15, 75, 275, ...
48 12687dd9 2023-08-04 jrmu (define (g-fives n)
49 12687dd9 2023-08-04 jrmu (cond
50 12687dd9 2023-08-04 jrmu [(zero? n) 3]
51 12687dd9 2023-08-04 jrmu [else (* 5 (g-fives (sub1 n)))]))
52 12687dd9 2023-08-04 jrmu
53 12687dd9 2023-08-04 jrmu ;g-fives-closed : N -> number
54 12687dd9 2023-08-04 jrmu ;Non-recursively returns the nth term of the geometric sequence: 3, 15, 75, 275, ...
55 12687dd9 2023-08-04 jrmu (define (g-fives-closed n)
56 12687dd9 2023-08-04 jrmu (* (expt 5 n) 3))
57 12687dd9 2023-08-04 jrmu
58 12687dd9 2023-08-04 jrmu ;seq-g-fives : N -> (listof numbers)
59 12687dd9 2023-08-04 jrmu ;Given n, returns a list of the first n-th terms in the g-fives sequence.
60 12687dd9 2023-08-04 jrmu
61 12687dd9 2023-08-04 jrmu (define (seq-g-fives n)
62 12687dd9 2023-08-04 jrmu (build-list n g-fives-closed))
63 12687dd9 2023-08-04 jrmu
64 12687dd9 2023-08-04 jrmu ;geometric-series : number number -> (N -> number)
65 12687dd9 2023-08-04 jrmu ;Given start and s, returns a function who computes the n-th term (where n is a natural number) of the geometric series with first term start with ratio s.
66 12687dd9 2023-08-04 jrmu
67 12687dd9 2023-08-04 jrmu (define (geometric-series start s)
68 12687dd9 2023-08-04 jrmu (local ((define (g-term n)
69 12687dd9 2023-08-04 jrmu (* (expt s n) start)))
70 12687dd9 2023-08-04 jrmu g-term))
71 12687dd9 2023-08-04 jrmu
72 12687dd9 2023-08-04 jrmu (define g-fives2 (geometric-series 3 5))