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-lambda-reader.ss" "lang")((modname |27.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 ;create-matrix : N (listof numbers) -> (listof (listof numbers))
5 12687dd9 2023-08-04 jrmu ;Given n (N[>=1]) and alon (alon has a length of n by m, where m is any integer), return a (listof (listof numbers)) representing an n by m matrix.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu (define (create-matrix n alon)
8 12687dd9 2023-08-04 jrmu (cond
9 12687dd9 2023-08-04 jrmu [(empty? alon) empty]
10 12687dd9 2023-08-04 jrmu [(zero? n) (error 'create-matrix "expected arg: nonzero number")]
11 12687dd9 2023-08-04 jrmu [(not (= (remainder (length alon) n) 0)) (error 'create-matrix "improper dimensions")]
12 12687dd9 2023-08-04 jrmu [else (cons (first-n n alon)
13 12687dd9 2023-08-04 jrmu (create-matrix n (remove-n n alon)))]))
14 12687dd9 2023-08-04 jrmu
15 12687dd9 2023-08-04 jrmu ;first-n : N (listof numbers) -> (listof numbers)
16 12687dd9 2023-08-04 jrmu ;Returns the first n numbers of alon as a (listof numbers).
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu (define (first-n n alon)
19 12687dd9 2023-08-04 jrmu (cond
20 12687dd9 2023-08-04 jrmu [(zero? n) empty]
21 12687dd9 2023-08-04 jrmu [else (cons (first alon)
22 12687dd9 2023-08-04 jrmu (first-n (sub1 n) (rest alon)))]))
23 12687dd9 2023-08-04 jrmu
24 12687dd9 2023-08-04 jrmu ;remove-n : N (listof numbers) -> (listof numbers)
25 12687dd9 2023-08-04 jrmu ;Removes the first n numbers of alon and returns the result as a (listof numbers).
26 12687dd9 2023-08-04 jrmu
27 12687dd9 2023-08-04 jrmu (define (remove-n n alon)
28 12687dd9 2023-08-04 jrmu (cond
29 12687dd9 2023-08-04 jrmu [(zero? n) alon]
30 12687dd9 2023-08-04 jrmu [else (remove-n (sub1 n) (rest alon))]))
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu (equal? (create-matrix 2 '(4 5 6 7 8 9)) '((4 5)
33 12687dd9 2023-08-04 jrmu (6 7)
34 12687dd9 2023-08-04 jrmu (8 9)))
35 12687dd9 2023-08-04 jrmu (create-matrix 3 '(4 5 6 7 8 9))