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-beginner-reader.ss" "lang")((modname 11.2.4) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;A deep-list is either
5 12687dd9 2023-08-04 jrmu ;1. a symbol or
6 12687dd9 2023-08-04 jrmu ;2. (cons dl empty) where dl is a deep-list.
7 12687dd9 2023-08-04 jrmu ;
8 12687dd9 2023-08-04 jrmu ;depth : deep-list -> number
9 12687dd9 2023-08-04 jrmu ;Consumes a deep-list dl and determines how many
10 12687dd9 2023-08-04 jrmu ;cons were used to construct it.
11 12687dd9 2023-08-04 jrmu ;
12 12687dd9 2023-08-04 jrmu ;Examples
13 12687dd9 2023-08-04 jrmu ;(depth (cons (cons (cons 'food empty) empty) empty))
14 12687dd9 2023-08-04 jrmu ;3
15 12687dd9 2023-08-04 jrmu ;(depth (cons 'food empty))
16 12687dd9 2023-08-04 jrmu ;1
17 12687dd9 2023-08-04 jrmu ;(depth 'food)
18 12687dd9 2023-08-04 jrmu ;0
19 12687dd9 2023-08-04 jrmu
20 12687dd9 2023-08-04 jrmu (define (depth dl)
21 12687dd9 2023-08-04 jrmu (cond
22 12687dd9 2023-08-04 jrmu [(symbol? dl) 0]
23 12687dd9 2023-08-04 jrmu [(cons? dl) (+ 1 (depth (first dl)))]))
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu ;Test
26 12687dd9 2023-08-04 jrmu ;(define dl1 (cons (cons (cons 'food empty) empty) empty))
27 12687dd9 2023-08-04 jrmu ;
28 12687dd9 2023-08-04 jrmu ;make-deep : symbol natural-number -> deep-list
29 12687dd9 2023-08-04 jrmu ;Consumes word and n to produce a deep-list
30 12687dd9 2023-08-04 jrmu ;containing word constructed using n cons's.
31 12687dd9 2023-08-04 jrmu ;
32 12687dd9 2023-08-04 jrmu ;Examples:
33 12687dd9 2023-08-04 jrmu ;(make-deep 'hey 3)
34 12687dd9 2023-08-04 jrmu ;(cons (cons (cons 'hey empty) empty) empty)
35 12687dd9 2023-08-04 jrmu ;(make-deep 'hey 0)
36 12687dd9 2023-08-04 jrmu ;'hey
37 12687dd9 2023-08-04 jrmu ;(make-deep 'hey 1)
38 12687dd9 2023-08-04 jrmu ;(cons 'hey empty)
39 12687dd9 2023-08-04 jrmu ;
40 12687dd9 2023-08-04 jrmu ;Template
41 12687dd9 2023-08-04 jrmu ;(define (make-deep word n)
42 12687dd9 2023-08-04 jrmu ; (cond
43 12687dd9 2023-08-04 jrmu ; [(zero? n) ...]
44 12687dd9 2023-08-04 jrmu ; [(> n 0) ... (make-deep word (sub1 n))]))
45 12687dd9 2023-08-04 jrmu
46 12687dd9 2023-08-04 jrmu (define (make-deep word n)
47 12687dd9 2023-08-04 jrmu (cond
48 12687dd9 2023-08-04 jrmu [(zero? n) word]
49 12687dd9 2023-08-04 jrmu [(> n 0) (cons (make-deep word (sub1 n)) empty)]))
50 12687dd9 2023-08-04 jrmu