HTDP Section 03

Tagged as HTDP, LISP, Self-Learning

Written on 2008-05-19 13:43:29

As promised, here’s Section 03 with 04 soon to follow. Sections 03 and 04 are pretty unremarkable and the questions and answers are pretty self-explanatory. Again, you can check all the latest code in my repo by going to manifest, then books, htdp, and navigating to the various source files.

Resources:
Read: Section 03
Watch: Nothing. To my knowledge there are no online lectures based around HTDP. Correct me if I’m wrong.
Checked against: Nothing. Again, to my knowledge there are no available sources to check your answers beyond the locked solutions on the official site and message boards. That’s one reason I’m excited about doing HTDP this way along with SICP. The plethora of SICP resources stand in contrast to an absolute dearth of resources for HTDP.

Exercises

3.1.1:

(define (attendees ticket-price)
(- 870 (* 150 ticket-price)))


This function will give incorrect answers for negative values of ticket-price.

3.1.2:

(define (revenue ticket-price)
(* (attendees ticket-price) ticket-price))

(define (costs ticket-price)
(+ 180 (* .04 (attendees ticket-price))))

(define (profit ticket-price)
(- (revenue ticket-price) (costs ticket-price)))


(profit 3) returns the best price which is 1063.2.

3.1.3:
Both program definitions return the same results for inputs of 3, 4
and 5.

3.1.4:

(define (profit ticket-price)
(- (revenue ticket-price)
(cost ticket-price)))

(define (revenue ticket-price)
(* (attendees ticket-price) ticket-price))

(define (cost ticket-price)
(* 1.5 (attendees ticket-price)))

(define (attendees ticket-price)
(+ 120
(* (/ 15 .10) (- 5.00 ticket-price))))

(define (profit price)
(- (* (+ 120
(* (/ 15 .10)
(- 5.00 price)))
price)
(* 1.5
(+ 120
(* (/ 15 .10)
(- 5.00 price))))))


Both programs return the same results but profit margins have changed based on the new costs. (max (profit 3) (profit 4) (profit 5) is now (profit 4).

3.2.1:

(define fixed-costs 180)
(define price-per-attendee .04)
(define start-attendees 120)
(define attendees-per-dime 15)
(define dime .10)
(define start-price 5.00)


3.3.1:

(define inches-in-cm 2.54)
(define inches-in-ft 12)
(define feet-in-yard 3)
(define yards-in-rod 5.5)
(define rods-in-furlong 40)
(define furlongs-in-mile 8)

(define (inches->cm inches)
(* inches-in-cm inches))

(define (feet->inches feet)
(* inches-in-ft feet))

(define (yards->feet yards)
(* feet-in-yard yards))

(define (rods->yards rods)
(* yards-in-rod rods))

(define (furlongs->rods furlongs)
(* rods-in-furlong furlongs))

(define (miles->furlongs miles)
(* furlongs-in-mile miles))

(define (feet->cm feet)
(inches->cm (feet->inches feet)))

(define (yards->cm yards)
(feet->cm (yards->feet yards)))

(define (rods->inches rods)
(feet->inches (yards->feet (rods->yards rods))))

(define (miles->feet miles)
(yards->feet (rods->yards (furlongs->yards
(miles-furlongs miles)))))


3.3.2:

(define pi 3.14159)

(define (volume-cylinder radius height)
(* pi (sqr radius) height))


3.3.3:

(define pi 3.14159)

(define (area-cylinder radius height)
(* 2 pi radius (+ radius height)))


3.3.4:

(define (area-pipe inner-radius length thickness)
(+ (* 2 pi length (+ inner-radius thickness))
(* 2 (- (* pi (+ inner-radius thickness))
(* pi inner-radius)))))

(define (area-pipe inner-radius length thickness)
(+ (area-pipe-side inner-radius length thickness)
(* 2 (area-pipe-ring inner-radius thickness))))

(define (area-pipe-side inner-radius length thickness)
(* 2 pi length (+ inner-radius thickness)))

(define (area-pipe-ring inner-radius thickness)
(* 2 (- (* pi (+ inner-radius thickness))
(* pi inner-radius))))


This problem reminds me of several in SICP in that the real difficulty with it is a misunderstanding of the question. Once you understand what is desired it’s pretty easy to bang the code out. This seems analogous to the idea that once you have a well-understood, well-specified set of requirements producing the code is trivial and that the requirements are the difficult part. Of course, this leads to blather about how good enough specifications (and UML Diagrams) are equivalent to code (which is bullshit). People forget that requirements change and that unambiguous well-specified requirements are often impossible.

3.3.5:

(define (height time)
(* .5 time (speed time)))

(define (speed time acceleration)
(* time acceleration))


3.3.6:

(define (Celsius->Fahrenheit cels)
(+ 32 (/ (* cels 9) 5)))

(I 32)
(Celsius->Fahrenheit (Fahrenheit->Celsius 32))
(Celsius->Fahrenheit (* (- 32 32) (/ 5 9)))
(Celsius->Fahrenheit (* 0 (/ 5 9)))
(Celsius->Fahrenheit 0)
(+ 32 (/ (* 0 9) 5))
(+ 32 (/ 0 5))
(+ 32 0)
32


Plainly, these functions are inverses of each other though that should be self evident. Since they are inverses their composition returns the original input. The stepper returns the same results.

Well, that’s it for Section 03. It seems that the first 8 Sections at least deal with language primitives and fairly basic material. It certainly is easier to progress through HTDP relative to SICP but I have had the sense that I was learning more in SICP. We’ll see if this changes at all once I progress beyond the early sections though I haven’t decided whether I’ll keep going through HTDP or forge ahead on SICP.
comments powered by Disqus

Unless otherwise credited all material Creative Commons License by Brit Butler