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-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 ;create-matrix : N (listof numbers) -> (listof (listof numbers))
5 ;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.
7 (define (create-matrix n alon)
8 (cond
9 [(empty? alon) empty]
10 [(zero? n) (error 'create-matrix "expected arg: nonzero number")]
11 [(not (= (remainder (length alon) n) 0)) (error 'create-matrix "improper dimensions")]
12 [else (cons (first-n n alon)
13 (create-matrix n (remove-n n alon)))]))
15 ;first-n : N (listof numbers) -> (listof numbers)
16 ;Returns the first n numbers of alon as a (listof numbers).
18 (define (first-n n alon)
19 (cond
20 [(zero? n) empty]
21 [else (cons (first alon)
22 (first-n (sub1 n) (rest alon)))]))
24 ;remove-n : N (listof numbers) -> (listof numbers)
25 ;Removes the first n numbers of alon and returns the result as a (listof numbers).
27 (define (remove-n n alon)
28 (cond
29 [(zero? n) alon]
30 [else (remove-n (sub1 n) (rest alon))]))
32 (equal? (create-matrix 2 '(4 5 6 7 8 9)) '((4 5)
33 (6 7)
34 (8 9)))
35 (create-matrix 3 '(4 5 6 7 8 9))