Tagged as HTDP, LISP, Self-Learning, SICP

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

Resources:

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.

Exercises

2.1.1:

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.

2.1.2:

(sqrt 4)

2

(sqrt 2)

#i1.4142135623730951

(sqrt -1)

0+1i

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

2.2.1:

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

2.2.2:

(define (dollar->euro dollars)

(* .642 dollars)) ;; as of 05/18/08

2.2.3:

(define (triangle side height)

(/ (* side height) 2))

2.2.4:

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

2.2.5:

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

2.3.1:

(define (tax income)

(* .15 income))

(define (netpay hrs)

(- (wage hrs) (tax (wage hrs))))

;;supplementary functions:

(define (wage hrs)

(* 12 hrs))

2.3.2:

(define (sum-coins pennies nickels dimes quarters)

(+ (* .01 pennies) (* .05 nickels) (* .1 dimes) (* .25 quarters)))

2.3.3:

(define (total-function attendees)

(- (* 5 attendees) (+ 20 (* .5 attendees))))

2.4.1:

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

2.4.2:

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

2.4.3:

;;> (+ 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

2.4.4:

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