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