User:Mchua/How to design programs/Pset 1

Installing Scheme

 * 1) Install DrScheme. This will also install Scheme on your computer.
 * 2) Read the Wikipedia article on the Scheme programming language.

Setting up a versioning system

 * 1) Install TortoiseSVN or an appropriate SVN client for your platform.

Polish notation
i.    (+ 1 2) = 3 ii. (- 5 3) = 2 iii. (* 2 2) = 4 iv. (* (+ (- 5 2) 2) 1) = 5 v.    (exp 2 (+ 2 1)) = 8
 * 1) Read the Wikipedia article on Polish Notation. Prefix Notation is the way that Scheme works. EVERYTHING is (operator parameter parameter).
 * 2) Evaluate the following expressions:

Scheme exercises
Work the following exercises as you're reading through the text. (Or, read the text, then go back and do the exercises.)


 * 2.1.1: Find out whether DrScheme has operations for...
 * squaring a number - (sqr x)
 * for computing the sine of an angle (sin angle-in-radians)
 * and for determining the maximum of two numbers (max a b)


 * 2.1.2: Evaluate in DrScheme...
 * (sqrt 4) = 2
 * (sqrt 2) = #i1.4142135623730951
 * (sqrt -1) = 0+1i
 * Then, find out whether DrScheme knows an operation for determining the tangent of an angle. (tan angle-in-radians)


 * 2.2.1 - Define the program Fahrenheit->Celsius,6 which consumes a temperature measured in Fahrenheit and produces the Celsius equivalent. Use a chemistry or physics book to look up the conversion formula.

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


 * 2.2.2 - Define the program dollar->euro, which consumes a number of dollars and produces the euro equivalent. Use the currency table in the newspaper to look up the current exchange rate.

(define (dollar->euro x) (* x 0.634437254))


 * 2.2.3 - Define the program triangle. It consumes the length of a triangle's side and the perpendicular height. The program produces the area of the triangle. Use a geometry book to look up the formula for computing the area of a triangle.

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


 * 2.2.4 - Define the program convert3. It consumes three digits, starting with the least significant digit, followed by the next most significant one, and so on. The program produces the corresponding number.

(define (convert3 a b c) (+ (* 100 c) (* 10 b) a))


 * 2.2.5 - skipped (I already know how to do these, and have a feel for the Scheme syntax and interpreter by now)
 * 2.3.1 - skipped
 * 2.3.2 - skipped
 * 2.3.3 - skipped

(These are good candidates for the lab notebook)


 * 2.4.1 - read and understand error messages for the following.

> (+ (10) 20) function call: expected a defined name or a primitive operation name after an open parenthesis, but found a number > (10 + 20) function call: expected a defined name or a primitive operation name after an open parenthesis, but found a number > (+ +) +: this primitive operator must be applied to arguments; expected an open parenthesis before the primitive operator name


 * 2.4.2 - Read the error messages, fix the offending definition in an appropriate manner, and repeat until all definitions are legal.

(define (f 1) (+ x 10))

>>> define: expected a name for the function's 1st argument, but found a number

(define (f x) (+ x 10))

(define (g x) + x 10)

>>> define: expected only one expression for the function body, but found at least one extra part

(define (g x) (+ x 10))

(define h(x)  (+ x 10))

>>> define: expected only one expression after the defined name h, but found one extra part

(define (h x)  (+ x 10))


 * 2.4.3 - Evaluate the following grammatically legal Scheme expressions in DrScheme's Interactions window:

(+ 5 (/ 1 0))

>>> /: division by zero

(sin 10 20)

>>> sin: expects 1 argument, given 2: 10 20

(somef 10)

>>> somef: name is not defined, not a parameter, and not a primitive name


 * 2.4.4 - Enter the following grammatically legal Scheme program into the Definitions window and click the Execute button.

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

Then, in the Interactions window, evaluate the expressions:

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

also note that DrScheme highlights the nesting level of the function that throws an error.

Section 2.5 is where the design recipe really comes in. Read carefully, take notes, create a "cheat sheet", if you like. (I had one on my wall for a while. What does yours look like?)


 * 3.1.1
 * 3.1.2
 * 3.1.3
 * 3.1.4

I'm going to skip these, because I can see how to do them immediately.

This is a powerful, yet simple, idea in computing: giving names to values. 3.2.1 asks you to do just that. If you're not convinced of what you're supposed to do or why, you should ask for help.


 * 3.2.1

''skipping. see above.''

These should have a full recipe followed:


 * 3.3.2
 * 3.3.3

''skipping. see above.''

The "stepper" is a powerful debugger. We'll use/discuss/reflect on this tool more. 3.3.6 is the first time you're explicitly asked to use it, I believe.


 * 3.3.6 - Recall the program Fahrenheit->Celsius from exercise 2.2.1. The program consumes a temperature measured in Fahrenheit and produces the Celsius equivalent. Develop the program Celsius->Fahrenheit, which consumes a temperature measured in Celsius and produces the Fahrenheit equivalent.

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

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

(define (I f) (Celsius->Fahrenheit (Fahrenheit->Celsius f)))

Evaluate (I 32) by hand and using DrScheme's stepper. What does this suggest about the composition of the two functions?

They're inverses.

Work through all the bits in sections 4 and 5. - done

Stop at section 6. The concepts and ideas change a bit.

Scheme problem set

 * 1) Complete hw1 Scheme exercises from HTDP.
 * 2) The file is in the repository. You will be able to read it once you have checked it out.
 * 3) I’d recommend making separate files for each problem (2.2.1.scm, 2.2.2.scm). Some are modifications of earlier programs, just do a save as.
 * 4) This took me about 4-6 hours to complete. I’d expect it takes you a similar amount. Break it into three or so two hour chunks. Talk someone else into going through it with you if you can. Pair programming (use VNC!) is awesome.

Reflection
So, the last questions are (from me) are reflective in nature:


 * 1) What syntaxes in Scheme have you encountered so far? - Prefix notation, syntax for defining variables and functions, conditionals.
 * 2) What types of data have you seen? - booleans, integers, fractions (with various ways of printing them), decimals, and representations of imaginary numbers... no strings, objects, or files yet.
 * 3) What kinds of programs can you write? Are they interesting? Why or why not? - So far, programs involving numerical input and computation, as well as boolean logic implementations. They can be mathematically interesting, but I want to learn how to write interfaces for people soon... oh, and how to do network stuff.