Content tagged HTDP

Report Card, Semester 1

posted on 2008-07-22 15:58:17

So, I've been trying to do this self-study thing for 30 weeks. I probably should've stepped back to evaluate my progress before now but I've allowed myself to be distracted with other things. You know, moving out, working my first full-time job, learning how to cook, clean and take care myself. That's no excuse though. Rather than beat around the bush some more let's just get to the heart of it:
"You got an F. What the hell's the matter with you? Ya big failure.
Final Grade: 20.786516853932586%
To be fair, you would've had to do 14.0 problems a week to finish the book in 26 weeks.
They are pretty hard problems. Just keep at it man. You may want to revise your strategy though."

We're 30 weeks into 2008 and I've only done 74 of the 356 problems in that legendary text, the Structure and Interpretation of Computer Programs, which was the central object of my study this semester. That's about two and a half problems a week. Not my brightest shining moment. This whole experience definitely gives me new appreciation for the people that tried to structure and/or educate me in the past. Clearly, I need one of two things:

1) A good kick in the ass to really get going.
2) A new gameplan.

Personally, I'm going to try a mix of the two. Where 1) is concerned I recently wrote a self-study program (the biggest program I've ever written, actually) to help me keep abreast of my own progress and help me chart my course a bit. Where 2) is concerned I'm going to have to start making concessions to maintain momentum and I'm not entirely comfortable with that.

What concessions do I mean? Well, some of the SICP problems are hard. Really hard. Unreasonably hard (see Exercise 4.79 at the bottom for which a good answer is "probably worth a Ph.D."). The book has it's reputation for a reason. It's a reputation of difficulty but also of enlightenment. A lot of very smart people say it's the best way to learn Computer Science and probably the best book on the subject yet written. I'm willing to take their word for it. Anyway, there are problems that I get hung up on and I haven't been letting myself move on to the next section of the book without solving all the problems in the current section. That just isn't scaling well. I'm already hung up on the last 4 problems in Section 2.1. God knows what would happen come 4.4. I'll surely never finish the thing if I don't let myself move forward.

With that in mind, a week or so ago I did let myself move forward a bit and work on Section 2.2. I've already got about a third of it done. Maybe even half. I'm worried about this because I want to stay honest. I don't want to shirk the hard stuff. I won't move past problems unless I'm really stumped and I will circle back at various points to try to work through them. Aside from SICP, I've worked on HTDP (How To Design Programs) and CA (Concrete Abstractions) as well this semester. I got an almost reasonable portion of HTDP done but next to nothing on CA. I'd really like to try plowing through as much of those three books and The C Programming Language (rocking the 1st ed.) as possible before Xmas.

Semester 3 (starting in January) I'm hoping to work on Algorithms (DPV, not CLRS), Essentials of Programming Languages (1st edition, baby!) and one of my Operating Systems texts. Of course, Discrete Math (5th ed) would be more prudent and judging by this semester this could all be revised by Xmas. Well, back to work. Happy Hacking!

HTDP Section 04

posted on 2008-05-19 14:13:12

Well, here's Section 04.

Read: Section 04
Watch: Nothing. To my knowledge there are no online lectures based around HTDP. Correct me if I’m wrong.
Checked against: Nothing.


1. (and true true) -> true
2. (or true false) -> true
3. (not false) -> true

1. (a) true, (b) false, (c) true
2. (a) false, (b) false, (c) true
3. (a) false, (b) false, (c) false


(define (is-between-3-and-7? n)
(and (> n 3) (<= n 10)))

(define (is-between-3-7? n)
(and (> n 3) (< n 10)))

(define (is-between-3-9? n)
(and (>= n 3) (< n 9)))

(define (is-1-3-or-9-11? n)
(or (is-1-3? n) (is-9-11? n)))

(define (is-1-3? n)
(and (> n 1) (< n 3)))

(define (is-9-11? n)
(and (> n 9) (< n 11)))

;;alternate implementation in case the first smacks of premature optimization:
;;(both suffer from an ominous arbitrary function naming schema!)
(define (is-1-3-or-9-11? n)
(or (and (> n 1) (< n 3))
(and (> n 9) (< n 11))))

(define (is-outside-1-3? n)
(not (and (>= n 1) (<= n 3))))


;; 1. | | | | | | | | | | |
;; -5 0 5
;; (-----)
;; Contract: in-interval-1? : number -> boolean
;; Purpose: To test if a number is between -3 and 0.
(in-interval-1? -2)
(and (< -3 -2) (< -2 0))
(and true true)

;;2. | | | | | | | | | | |
;; 0 5 10
;; --) (----------------
;; Contract: in-interval-2? : number -> boolean
;; Purpose: To test if a number is less than 1 or greater than 2.
(in-interval-2? -2)
(or (< -2 1) (> -2 2))
(or true false)

;;3. | | | | | | | | | | |
;; 0 5 10
;; --) (----------
;; Contract: in-interval-3? : number -> boolean
;; Purpose: To test if a number is less than 1 or greater than 5.
(in-interval-3? -2)
(not (and (<= 1 -2) (<= -2 5)))
(not (and false true))
(not false)


(define (is-solution-1? x)
(= (+ (* 4 x) 2) 62))

(define (is-solution-2? x)
(= (* (sqr x) 2) 102))

(define (is-solution-3? x)
(= (+ 2 (* 4 (sqr x)) (* 6 x)) 462))

10 is a solution to 3. 12 and 14 are not solutions.


;; I don't know what specific test cases the authors are referring to for problems 2.2.1 - 2.2.4 so I'll just make up a few.
(= (Fahrenheit->Celsius 32) 0)
(= (dollar->euro 20) 12.8399) ;; as of 05/18/08
(= (triangle 5 2) 5)
(= (convert3 9 2 7) 729)

The left cond is legal. The right cond is illegal because it's second clause has no answer to evaluate. The last cond is illegal because it has no second clause to evaluate.

(a) .040
(b) .045
(c) .060

(a) 40
(b) 121
(c) 595


(define (interest x)
(cond ((<= x 1000) (* .04 x))
((<= x 5000) (* .045 x))
(else (* .05 x))))


(define (tax x)
(cond ((<= x 240) 0)
((<= x 480) (* .15 x))
(else (* .28 x))))

(define (netpay hrs)
(- (grosspay hrs) (tax (grosspay hrs))))

(define (grosspay hrs)
(* 12 hrs))


(define (pay-back charges)
(cond ((<= charges 500) (* .025 charges))
((<= charges 1500) (* .05 charges))
((<= charges 2500) (* .075 charges))
(else (* .01 charges))))


(define (how-many a b c)
(cond ((> (sqr b) (* 4 a c)) 2)
((= (sqr b) (* 4 a c)) 1)
((< (sqr b) (* 4 a c)) 0))) ;; or else 0))
;; (how-many 1 0 1) = 0

If we didn't assume the equation was proper we'd need to check (with
a cond) to see if a equaled 0 and return an error if it did.

That does it for Section 04. Hopefully, I'll get my act together and wrap up SICP Section 2.1 in the next week or so. :-) You've gotta work on some hard stuff too, right? Besides it's more interesting anyway.

HTDP Section 03

posted 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.

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.



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

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


(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.

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


(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)))
(* 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).


(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)


(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)))))


(define pi 3.14159)

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


(define pi 3.14159)

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


(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.


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

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


(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)

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.

HTDP Section 02

posted on 2008-05-19 03:25:27

So, I've finally gotten around to cleaning up SICP Section 1.3. It's not quite done but it's damn close. For now, I want to start posting some of the HTDP code I've been writing to get back in the hacking habit over the past few days. I also have some of Concrete Abstractions done and in my source code repository but it's nothing substantial. Without further ado, here's HTDP Section 02 (of 43!). Sections 03 and 04 will go up tomorrow. Note: I skipped HTDP Section 01 because there are no exercises or problems whatsoever.


Read: Sections 01 and 02

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.



Dr. Scheme does have operations for squaring (sqr x), computing sines (sin x), and finding maximums (max x). If you are not running in the HTDP Beginning Student Language though these functions may not be available.


(sqrt 4)
(sqrt 2)
(sqrt -1)

;;(tan x) determines the tangent of a given angle.


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

The teachpack worked as intended. Just go to Language -> Add Teachpack. Feel free to test the different convert-*s on your own.


(define (dollar->euro dollars)
(* .642 dollars)) ;; as of 05/18/08


(define (triangle side height)
(/ (* side height) 2))


(define (convert3 first second third)
(+ (* 100 third) (* 10 second) (* 1 first)))

This was sort of counter-intuitive. The idea that this is related to something in an Algebra book is true but misleadingly so. You could try to do something fancy with max but that's not the idea.


(define (f n)
(+ (/ n 3) 2))

;;The evaluations for 2, 5, and 9 are 2.6, 3.6 and 5, respectively.

(define (f n)
(+ 10 (sqr n)))

;;The evaluations for 2 and 9 are 14 and 91, respectively.

(define (f n)
(+ 20 (* (sqr n) .5)))

;;The evaluations for 2 and 9 are 22 and 60.5, respectively.

(define (f n)
(- 2 (/ 1 n)))

;;The evaluations for 2 and 9 are 1.5 and 1.8, respectively.


(define (tax income)
(* .15 income))

(define (netpay hrs)
(- (wage hrs) (tax (wage hrs))))

;;supplementary functions:
(define (wage hrs)
(* 12 hrs))


(define (sum-coins pennies nickels dimes quarters)
(+ (* .01 pennies) (* .05 nickels) (* .1 dimes) (* .25 quarters)))


(define (total-function attendees)
(- (* 5 attendees) (+ 20 (* .5 attendees))))


(10) causes the interpreter to expect a function, procedure or expression but it is in fact primitive data, i.e. a number.

(10 + 20) is incorrect because the expression uses infix rather than prefix notation but the error from the interpreter is the same. This is due to the fact that the interpreter has been given a number rather than an procedure as it's operator.

(+ +) fails because the operator + is only given one argument (it requires a minimum of two) and that argument is a function which is the wrong type of input.


(define (f x)
(+ x 10))
;;The argument to f needed to be changed.

(define (g x)
(+ x 10))
;;There was a missing open-paren before the + operator.

(define (h x)
(+ x 10))
;;The open-paren was in front of x when it should have been in front of h.


;;> (+ 5 (/ 1 0))
;;/: division by zero
;;> (sin 10 20)
;;sin: expects 1 argument, given 2: 10 20
;;> (somef 10)
;;reference to an identifier before its definition: somef


(define (somef x)
(sin x x))

;;> (somef 10 20)
;;somef: this procedure expects 1 argument, here it is provided 2 arguments
;;> (somef 10)
;;sin: expects 1 argument, given 2: 10 10

The section ends with a bit on program design. It makes the important note of having human solved examples to test against. Sounds like an argument for unit tests to me.

Adulthood and Academics

posted on 2008-05-14 03:39:37

Admittedly, the title of this post is a misnomer. I'm no adult yet. I am trying my damnedest to keep this house running smoothly though. As of today I think we're up on all the utilities and I've got a Static IP here with AT&T so I can move my server at some point. Work's been going well and I'm taking MARTA in to simplify my life a bit. I'm cooking (if you can call it that) and keeping the dishes done and the house clean with regularity. I'd say I've almost settled into a groove. I say almost because the people actually staying in the house won't stabilize until after May 25th. Heck, even I'm gone from the 17th to the 24th to house sit for my parents. For the most part though I'm enjoying myself.

Additionally,  I'm way behind on programming. I know. I've had a lot going on but my progress the last month or two is still just shameful. I've started HTDP to get the juices flowing again and am already through Section 03. It's definitely more straightforward than SICP if less revelatory. I'm considering going ahead and trying to blow through HTDP completely over the next month or two. Then I could circle back to SICP and hopefully be better prepared. I haven't decided on anything yet other than tidying up the presently unadorned answers to SICP 1.3 and then posting what I've got from HTDP so far. I am more than half-way through SICP 2.1 but I'm wondering if it makes more sense to knock out HTDP considering the difference in pace between the books. I'll let you know as I move forward. I'm hoping to get a post up with some pictures of my new digs in the next week or so. Feel free to drop me a line if you'd like to swing by.

Latest Learnings: Lessig and Lisp

posted on 2008-01-16 04:34:47

So, I've been meaning to post about the things I taught myself in Montana and my course of study and a course schedule through May/syllabus but I had to recover some partitions on my desktop. My laptop also was caught between Ubuntu Hardy Alpha 2 and Alpha 3. I'm not getting into it. It's a long story. Anyway, now that all my systems are running flawlessly I'll speak a little on the aforementioned subject matter.

I have a long term plan for a course of study but no hard schedule yet. I have to divide up readings and problem sets and link them with lectures and such. I plan to have such a syllabus done and up for viewing by the end of the week. As for the long term plan of study there are 6 Programming Texts and 3 Math Texts that I'd really like to get through. If I get through the first 3 Programming Texts (or even the first 2) and 1 or 2 of the Math Texts I'd consider it a successful year. They're all fairly rigorous and I'd like to cover them in depth. Of late, I've been debating the order in which to approach the programming texts. Either SICP, CTM, HTDP or HTDP, CTM, SICP. Some of the stuff in SICP is a bit difficult and some of the stuff in HTDP is a bit easy so far. This is another thing I'm hoping to have worked out by the end of the week so that I can get going.

Once I do have a syllabus I'll post it and then post notes on readings and lectures and solutions to exercises as I go along so feel free to follow along and ask questions. You can only help me learn more. So far, I read the first 40 pages of SICP in Montana. That's Chapter 1 (of 5), Section 1.1. I've got notes typed up on the lecture and reading and most of the examples solved. I'll get those posted up by Friday as the first entry whatever my course of study turns out to be. Also, Friday I will be going to that Yeasayer concert. So far Ben Grad and Minor are talking about going too. Any more takers? Have you guys liked Yeasayer as much as I have? Isn't that Red Cave song from yesterday awesome?

Finally, here are some good Lessig quotes my readings in Montana of The Future of Ideas (Pgs. 1 - 99):

"The very idea that nonexclusive rights might be more efficient than exclusive rights rarely enters the debate. The assumption is control, and public policy is dedicated to maximizing control." - Lawrence Lessig, The Future of Ideas, Pg. 86

"Where we have little understanding about how a resource will be used, we have more reason to keep that resource in the commons. And where we have a clear vision of how a resource will be used, we have more reason to shift that resource to a system of control." - Lawrence Lessig, The Future of Ideas, Pg. 88-89

"The point is more than theoretical. In essence, the changes in the environment of the Internet that we are observing now alter the balance between control and freedom on the Net. The tilt of these changes is pronounced: control is increasing. And while one cannot say in the abstract that increased control is a mistake, it is clear that we are expanding this control with no sense of what is lost. The shift is not occurring with the idea of a balance in mind. Instead, the shift proceeds as if control were the only value." - Lawrence Lessig, The Future of Ideas, Pg. 99

Unless otherwise credited all material Creative Commons License by Brit Butler