Friday, March 13, 2009

the first 3 chapters's excersises of htdp

I begun to read the htdp about 1 week ago.It's a amazing book that give you a way to explain what is computing by simplicity.The implementation of the computing cases are used by a little old computer language which is scheme that invented by Gerald Sussman who is a great computer science professor in MIT.Lisp is the second oldest language in computer field which designed by John McCarthy who is a student of Alonzo Church that he is a author of lambda calculus.Scheme is one of the best dialect of lisp.

How to Design Program?
perhaps people got their answers.As a newbie monkey coder thought that htdp would give you a big help.You have to forget what you knew.A lots sucks infos in your mind aNd you need blow it away.Like Sussman said"it's not about computer and's all about computing."When I was a student in college,people just talk about j2EE,.net and microsfot something.That almostly bullshit!We need looking for the truth from computing world.htdp and sicp would provide a chance to know that truth,make a choice?
btw:words from a friend(non-christian) suddenly came into my mind"Even for our next generations kids,we have to hold the Free Software hood."

I was picks some interesting excersises,let's see:
;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "" "lang")((modname fah) (read-case-sensitive #t) (teachpacks ((lib "" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "" "teachpack" "htdp")))))
;Exercise 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 (lambda
(* (/ 5 9) (- Fahrenheit 32))))

; Contract: area-of-ring : number number -> number
; Purpose: to compute the area of a ring whose radius is
; outer and whose hole has a radius of inner
; Example: (area-of-ring 5 3) should produce 50.24
; Definition: [refines the header]

(define area (lambda (r)
(* 3.14 (* r r))))

(define area-of-ring (lambda (outer inner)
(- (area outer)
(area inner))))

;Exercise 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. For example, the expected value of
; (convert3 1 2 3)
;is 321. Use an algebra book to find out how such a conversion works.

(define convert3 (lambda (x y z)
(+ (* x 1) (* y 10) (* z 100))))

Exercise 3.1.4. After studying the cost structure of a show, the owner discovered several ways
of lowering the cost. As a result of his improvements, he no longer has a fixed cost. He now
simply pays $1.50 per attendee.

; profit : number -> number
; to compute the profit as the difference between revenue and costs
; at some given ticket-price
(define profit (lambda (ticket-price)
(- (revenue ticket-price)
(cost ticket-price))))

;; revenue : number -> number
;; to compute the revenue, given ticket-price
(define revenue (lambda (ticket-price)
(* (attendees ticket-price) ticket-price)))

;; cost : number -> number
;; to compute the costs, given ticket-price
(define cost (lambda (ticket-price)
(* 1.5 (attendees ticket-price))))

;; attendees : number -> number
;; to compute the number of attendees, given ticket-price
(define attendees (lambda (ticket-price)
(+ 120
(* (/ 15 0.1) (- 5 ticket-price)))))

;Exercise 3.3.1. The United States uses the English system of (length) measurements. The rest
;of the world uses the metric system. So, people who travel abroad and companies that trade with
;foreign partners often need to convert English measurements to metric ones and vice versa.
;Here is a table that shows the six major units of length measurements of the English system:
; English metric
; 1 inch = 2.54 cm
; 1 foot = 12 in.
; 1 yard = 3 ft.
; 1 rod = 5(1/2) yd.
; 1 furlong = 40 rd.
; 1 mile =8 fl.
;Develop the functions inches->cm, feet->inches, yards->feet, rods->yards, furlongs->rods, and miles->furlongs.

(define inch-cm 2.54)
(define foot-inch 12)
(define yard-feet 3)
(define rod-yard 5.5)
(define furlong-rod 40)
(define mile-furlong 8)

;;functions definition
(define inches->cm (lambda (inches)
(* inches inch-cm)))

(define feet->inches (lambda (feet)
(* feet foot-inch)))

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

(define rods->yards (lambda (rods)
(* rods rod-yard)))

(define furlongs->rods (lambda (furlongs)
(* furlongs furlong-rod)))

(define miles->furlongs (lambda (miles)
(* miles mile-furlong)))

;Then develop the functions feet->cm, yards->cm, rods->inches, and miles->feet.
(define feet->cm (lambda (feet)
(feet->inches feet))))

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

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

(define miles->feet (lambda (miles)
(miles->furlongs miles))))))

;Exercise 3.3.5. Develop the program height, which computes the height that a rocket reaches
;in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g · t
;in t time units and a height of 1/2 * v * t where v is the speed at t.

;a constant for acceleration
(define g 5)

;speed of g * t
(define speed (lambda (t)
(* g t)))

;height of /1/2 * v * t
(define height (lambda (t)
(* 0.5 (speed t) t)))

1 comment:

Runescape 2007 Gold said...

i appreciate your blog,its really very great one this one very helpful blog .the i'll absolutely get lot's of information regarding seo process that how it works.

RS 3 Gold
cheapest runescape gold
Gold für World of Warcraft