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 |26.1|) (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 ;;tabulate-div : N -> (listof N)
5 12687dd9 2023-08-04 jrmu ;;Given n, tabulates all the divisors of n starting from 1 and ending in n.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu ;;Examples:
8 12687dd9 2023-08-04 jrmu
9 12687dd9 2023-08-04 jrmu ;;(equal? (tabulate-div 5) '(1 5))
10 12687dd9 2023-08-04 jrmu ;;(equal? (tabulate-div 10) '(1 2 5 10))
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;;Using general list processing functions:
13 12687dd9 2023-08-04 jrmu ;;Generate a list of integers from 1 to n who are potential divisors, then filter for those that divide evenly into n.
14 12687dd9 2023-08-04 jrmu
15 12687dd9 2023-08-04 jrmu (define (tabulate-div1 n)
16 12687dd9 2023-08-04 jrmu (filter (lambda (x) (= (remainder n x) 0))
17 12687dd9 2023-08-04 jrmu (build-list n (lambda (x) (+ 1 x)))))
18 12687dd9 2023-08-04 jrmu
19 12687dd9 2023-08-04 jrmu ;;Using structural recursion:
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu ;;return-divisors : N N -> (listof N)
22 12687dd9 2023-08-04 jrmu ;;returns a (listof N) which contains the list of the divisors for the number n from 1 up to d (inclusive).
23 12687dd9 2023-08-04 jrmu
24 12687dd9 2023-08-04 jrmu (define (tabulate-div2 n)
25 12687dd9 2023-08-04 jrmu (local ((define (return-divisors n d)
26 12687dd9 2023-08-04 jrmu (cond
27 12687dd9 2023-08-04 jrmu [(= d 1) (list 1)]
28 12687dd9 2023-08-04 jrmu [(= (remainder n d) 0) (cons d (return-divisors n (sub1 d)))]
29 12687dd9 2023-08-04 jrmu [else (return-divisors n (sub1 d))])))
30 12687dd9 2023-08-04 jrmu (reverse (return-divisors n n))))
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu ;(time (tabulate-div1 299400))
33 12687dd9 2023-08-04 jrmu ;(time (tabulate-div2 299400))
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu ;merge-sort : (listof numbers) -> (listof numbers)
36 12687dd9 2023-08-04 jrmu ;Sorts alon in ascending order.
37 12687dd9 2023-08-04 jrmu ;
38 12687dd9 2023-08-04 jrmu ;make-singles : (listof X) -> (listof (listof X))
39 12687dd9 2023-08-04 jrmu ;Given alox, creates a listof one number lists.
40 12687dd9 2023-08-04 jrmu
41 12687dd9 2023-08-04 jrmu (define (make-singles alox)
42 12687dd9 2023-08-04 jrmu (cond
43 12687dd9 2023-08-04 jrmu [(empty? alox) empty]
44 12687dd9 2023-08-04 jrmu [else (append (list (list (first alox)))
45 12687dd9 2023-08-04 jrmu (make-singles (rest alox)))]))
46 12687dd9 2023-08-04 jrmu
47 12687dd9 2023-08-04 jrmu ;merge-all-neighbors : (listof (listof X)) -> (listof X)
48 12687dd9 2023-08-04 jrmu ;Given alolox, recursively merge neighbors to return a (listof X).
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu ;Examples:
51 12687dd9 2023-08-04 jrmu ;(merge-all-neighbors (list (list 2 5) (list 3 9)))
52 12687dd9 2023-08-04 jrmu ;(list 2 5 3 9)
53 12687dd9 2023-08-04 jrmu
54 12687dd9 2023-08-04 jrmu (define (merge-all-neighbors alolox)
55 12687dd9 2023-08-04 jrmu (cond
56 12687dd9 2023-08-04 jrmu [(cons? (first alolox))]
57 12687dd9 2023-08-04 jrmu []))