Learn faster and stay on-track by joining this free class with other self-learners.
|
How to Design ProgramsClass length: 1 weeks. Start anytime. Creator: michael_michael Status: Under Construction |
Join this class! |
|
Lesson 1: Assignment 1Post your answers to the exercises in section 2 and briefly summarize key points (2-3 sentences) for each of the following sections:
Homework Submissions3 total2.1 Scheme respects numbers - when they are inexact, it carefully tells us instead of dumping wrong output like most other languages. It understand rational numbers and complex numbers by default. 2.2 Almost anything can be in a variable or program name. There's a close correspondence between a Scheme 'program' and a mathematical 'function' - convert-gui and Ex 2.2.5 express this beautifully. 2.3 Interpreting wordy specifications to precise terms and making programs out of them is an important job of programmers. 2.4 Do not be sloppy about your parens. 2.5 Start your program with a contract and examples, write it, then test with the examples themselves. (sqr 2) (sin (/ pi 2)) (max 2 42) 2 ; this is (sqrt 4) #i1.4142135623730951 ; this is (sqrt 2) 0+1i ; this is (sqrt -1) (tan (/ pi 4)) (define (fahr->celsius fahrenheit) (* (- fahrenheit 32) 5/9)) (define (dollar->euro dollarval) (* 0.738770686 dollarval)) (define (triangle side height) (* 1/2 side height)) (define (convert3 ones tens hundreds) (+ (* 100 hundreds) (* 10 tens) ones)) (define (f n) (+ (/ n 3) 2)) (define (f1 n) (+ (sqr n) 10)) (define (f2 n) (+ (* 1/2 (sqr n)) 20)) (define (f3 n) (- 2 (/ 1 n))) (define (tax pay) (* pay 15/100)) (define (netpay-wrong hours) (* hours 12)) ;I wrote this corrected version after reading it again and seeing the difference 'gross pay' and 'net pay'. (define (netpay hours) (- (* hours 12) (tax (* hours 12)))) (define (sum-coins pennies dimes nickels quarters) (+ pennies (* 10 dimes) (* 20 nickels) (* 25 quarters))) (define (total-profit num-attendees) (- (* 5 num-attendees) (+ 20 (* 0.5 num-attendees)))) ;; area-of-circle number -> number ;; to compute the area of a circle given its radius ;; example: (area-of-circle 5) should be 78.539 (define (area-of-circle radius) (* pi (sqr radius))) (area-of-circle 5) "should be" 78.539 ;; area-of-ring number, number -> number ;; to compute the area of a ring, given the ;; radii of the hole (inner) and the full radius (outer) ;; example: (area-of-ring 3 5) should be 50.24 (define (area-of-ring inner outer) (- (area-of-circle outer) (area-of-circle inner))) (area-of-ring 3 5) "should be" 50.24 No comments. Sign up or log in to comment Brief summaries: 2.1 Numbers and Arithmetic Scheme permits the input of data as well as its manipulation through primitive operations (+ - * etc.). Scheme differentiates between exact numbers and inexact numbers. Inexact numbers may represent infinitely repeating decimals, for example. 2.2 Variables and Programs Scheme allows us to use placeholders to stand in for unknown quantities, or variables. A variable can consume an input and act on it. 2.3 Word Problems Programs often represent solutions to word problems. One important key to designing programs is effectively extracting relevant information from a word problem. 2.4 Errors Atomic expressions are composed of numbers and variables, whereas compound expressions may be composed of an operation, variables, and/or more expressions. Compound expressions are surrounded by parentheses. We can distinguish between three types of errors: Syntax errors (expressions that can't be evaluated because they are malformed), run-time errors (an expression that is faulty because of it is mathematically nonsensical, for example), and logical errors (the program or expression is well-formed, but it produces incorrect results). 2.5 Designing Programs Designing a program can be boiled down into a 'recipe' consisting of four phases: contract purpose and header, examples, body, and test # Exercise 2.1.1. # Find out whether DrScheme has operations for squaring a number# 'Yes, DrScheme has a sqr operation (sqr x)' # For computing the sine of an angle# 'Yes, (sin x)' # And for determining the maximum of two numbers. 'Yes, (<>= x x) provide comparative operations.' # Exercise 2.1.2. # Evaluate (sqrt 4), (sqrt 2), and (sqrt -1) in DrScheme. # 2, #i1.4142135623730951, 0+1i # Then, find out whether DrScheme knows an operation for determining the tangent of an angle. 'Yes, (tan x)' # Exercise 2.2.1. Define the program Fahrenheit->Celsius,6 which consumes a temperature measured in Fahrenheit and produces the Celsius equivalent. (define (fahrenheit->celsius f) (/ (- f 32) 1.8)) (define (dollar->euro d) (* d .74)) (define (triangle b h) (* (* b h) .5)) (define (convert a b c) (+ a (* b 10) (* c 100))) (define (n^2+2 n) (+ (* n n) 2)) (define (.5*n^2+20 n) (+ 20 (* .5 (* n n)))) # Exercise 2.3.1. Utopia's tax accountants always use programs that compute # income taxes even though the tax rate is a solid, never-changing 15%. Define # the program tax, which determines the tax on the gross pay. # # Also define netpay. The program determines the net pay of an employee from the # number of hours worked. Assume an hourly rate of $12 (define (tax pay) (* pay .85)) (define (netpay hours) (* 12 (tax hours))) # Exercise 2.3.2. The local supermarket needs a program that can compute the # value of a bag of coins. Define the program sum-coins. It consumes four # numbers: the number of pennies, nickels, dimes, and quarters in the bag; it # produces the amount of money in the bag. (define (coin-value p n d q) (+ p (* n 5) (* d 10) (* q 25))) # Exercise 2.3.3. An old-style movie theater has a simple profit function. # Each customer pays $5 per ticket. Every performance costs the theater $20, # plus $.50 per attendee. Develop the function total-profit. It consumes the # number of attendees (of a show) and produces how much income the attendees # produce. (define (theater-profit n) (+ (* n 5) (* n -.5) -20)) # Exercise 2.4.2. Enter the following sentences, one by one, into DrScheme's # Definitions window and click Execute. Fix syntax errors: # # (define (f 1) (+ x 10)) (define (f x) (+ x 10)) # (define (g x) + x 10) (define (g x) (+ x 10)) # (define h(x) (+ x 10)) (define (h x) (+ x 10)) Comments:
Actually, infinitely repeating decimals will be rational numbers (if I remember my math correctly), and so by default wouldn't be inexact. It's the "irrational" numbers, which can't be expressed as repeating decimals, that are by default inexact.
There seems to be an inbuilt (max x1 x2 ...) function, which seems a more appropriate answer.
Scheme operators mostly are not limited to being binary (two operands), so this can be better written as (* b h .5). Markdown messed things up, so: the first, third and fifth paragraphs are supposed to be quotes. And the fifth paragraph should actually has asterisk signs like: (* (* b h) .5). |
No comments. Sign up or log in to comment