1.3 Converting Temperatures: "convert.rkt"

► |  | How to Design Programs Teachpacks

1 | Ht DP Teachpacks
2 | Ht DP/ 2e Teachpacks

▼ | 1 | Ht DP Teachpacks

1.1 | Manipulating Images: "image.rkt"
1.2 | Simulations and Animations: "world.rkt"
1.3 | Converting Temperatures: "convert.rkt"
1.4 | Guessing Numbers: "guess.rkt"
1.5 | Master Minding: "master.rkt"
1.6 | Playing Master Mind: "master-play.rkt"
1.7 | Simple Drawing: "draw.rkt"
1.8 | Hangman: "hangman.rkt"
1.9 | Playing Hangman: "hangman-play.rkt"
1.10 | Managing Control Arrows: "arrow.rkt"
1.11 | Manipulating Simple HTML Documents: "docs.rkt"
1.12 | Working with Files and Directories: "dir.rkt"
1.13 | Graphing Functions: "graphing.rkt"
1.14 | Simple Graphical User Interfaces: "gui.rkt"
1.15 | An Arrow GUI: "arrow-gui.rkt"
1.16 | Controlling an Elevator: "elevator.rkt"
1.17 | Lookup GUI: "lkup-gui.rkt"
1.18 | Guess GUI: "guess-gui.rkt"
1.19 | Queens: "show-queen.rkt"
1.20 | Matrix Functions: "matrix.rkt"

On this page:

convert-gui
convert-repl
convert-file

top ← prev up next →

#### 1.3`` Converting Temperatures: "convert.rkt"

(require htdp/convert)

The teachpack convert.rkt provides three functions for converting Fahrenheit temperatures to Celsius. It is useful for a single exercise in HtDP. Its purpose is to demonstrate the independence of “form” (user interface) and “function” (also known as “model”).

> procedure (convert-gui convert) → true
> convert : (-> number? number?)

Consumes a conversion function from Fahrenheit to Celsius and creates a graphical user interface with two rulers, which users can use to convert temperatures according to the given temperature conversion function.

> procedure (convert-repl convert) → true
> convert : (-> number? number?)

Consumes a conversion function from Fahrenheit to Celsius and then starts a read-evaluate-print loop. The loop prompts users to enter a number and then converts the number according to the given temperature conversion function. A user can exit the loop by entering “x.”

> procedure (convert-file in convert out) → true
> in : string?
> convert : (-> number? number?)
> out : string?

Consumes a file name in, a conversion function from Fahrenheit to Celsius, and a string out. The program then reads all the number from in, converts them according to convert, and prints the results to the newly created file out.

Warning: If out already exists, it is deleted.

Example: Create a file with name "in.dat" with some numbers in it, using your favorite text editor on your computer. Define a function f2c in the Definitions window and set teachpack to "convert.rkt" and click Run. Then evaluate

> (convert-gui f2c)
> ; and
> (convert-file "in.dat" f2c "out.dat")
> ; and
> (convert-repl f2c)

Finally inspect the file "out.dat" and use the repl to check the answers.
top ← prev up next →