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-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 ;A deep-list is either
5 ;1. a symbol or
6 ;2. (cons dl empty) where dl is a deep-list.
7 ;
8 ;depth : deep-list -> number
9 ;Consumes a deep-list dl and determines how many
10 ;cons were used to construct it.
11 ;
12 ;Examples
13 ;(depth (cons (cons (cons 'food empty) empty) empty))
14 ;3
15 ;(depth (cons 'food empty))
16 ;1
17 ;(depth 'food)
18 ;0
20 (define (depth dl)
21 (cond
22 [(symbol? dl) 0]
23 [(cons? dl) (+ 1 (depth (first dl)))]))
25 ;Test
26 ;(define dl1 (cons (cons (cons 'food empty) empty) empty))
27 ;
28 ;make-deep : symbol natural-number -> deep-list
29 ;Consumes word and n to produce a deep-list
30 ;containing word constructed using n cons's.
31 ;
32 ;Examples:
33 ;(make-deep 'hey 3)
34 ;(cons (cons (cons 'hey empty) empty) empty)
35 ;(make-deep 'hey 0)
36 ;'hey
37 ;(make-deep 'hey 1)
38 ;(cons 'hey empty)
39 ;
40 ;Template
41 ;(define (make-deep word n)
42 ; (cond
43 ; [(zero? n) ...]
44 ; [(> n 0) ... (make-deep word (sub1 n))]))
46 (define (make-deep word n)
47 (cond
48 [(zero? n) word]
49 [(> n 0) (cons (make-deep word (sub1 n)) empty)]))