Whatever you are learning or writing, keep in mind that every language
was created with a set of principles and use cases in mind - some
more than others.
Learn to reject your ego. Learn to love burning code.
Name things what they are. Begin with your program’s final state in mind!
Some programs are as small as essays. Others are like sets of encyclopedias. Writing good essays and books requires careful planning, and writing good programs does, too. Small or large, a good program cannot be created by tinkering around. It must be carefully designed.
; Exercise 2.2.1. Define the program Fahrenheit->Celsius,; 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->celsiusx)(* (- x32)(/ 59)))(convert-guifahrenheit->celsius)
Scheme allows you to represent fractions directly as well:
(define (fahrenheit->celsiusx)(* (- x32)5/9))
…though I’m not a huge fan of that.
Inexact numbers are represented with an #i in front.
Here’s a word problem and my solution:
Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work.
My solution is slightly more efficient than
the solution
as it does not recompute the gross pay by including the subtract-taxes function.
Designing programs is something that must be done intentionally and with care. SICP provides a Design Recipe which can be followed to yield reliably useful programs.
We need to determine what’s relevant in the problem statement and what we can ignore. We need to understand what the program consumes, what it produces, and how it relates inputs to outputs. We must know, or find out, whether Scheme provides certain basic operations for the data that our program is to process. If not, we might have to develop auxiliary programs that implement these operations. Finally, once we have a program, we must check whether it actually performs the intended computation.
;; 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-of-ringouterinner)(- (area-of-diskouter)(area-of-diskinner)));; Tests:(area-of-ring53);; expected value50.24
A summary of these phases is provided in
Figure 4
in this section of the textbook, but I’ll summarize here quickly:
The Contract names the function and specifies the class of the input and output data
The Purpose explains the name
The Example lists a sample use case
The Definition is a scheme expression
Finally, Tests ensure output is correct
(Funny, Elixir has real good support for inline tests with the doctest feature. Makes me miss Elixir.)
3 Programs are Function Plus Variable Definitions¶
9.1 Lists
9.2 Data Definitions for Lists of Arbitrary Length
9.3 Processing Lists of Arbitrary Length
9.4 Designing Functions for Self-Referential Data Definitions
9.5 More on Processing Simple Lists