How to Design Programs, Second Edition——Chapter 1 Fixed-Size Data 固定尺寸数据

本文深入探讨了四种基本数据类型(数字、字符串、布尔值和图像)的算术运算,包括数字的加减乘除、字符串的拼接、图像的创建和参数查询,以及布尔值的逻辑运算。通过一系列的练习,读者可以掌握如何在这些数据类型间进行混合运算,并了解函数定义和程序的基本构造。此外,还介绍了如何利用DrRacket的stepper进行逐步计算以理解函数执行过程。
摘要由CSDN通过智能技术生成

1 Arithmetic 算数

四种原子数据

  1. numbers
  2. strings
  3. boolean values
  4. images
-----------------------------------------

1.1 The Arithmetic of Numbers 

Exercise 1. Add the following definitions for  x and  y to DrRacket’s definitions area:
(define x 3)
(define y 4)
Now imagine that  x and  y are the coordinates of a Cartesian point. Write down an expression that computes the distance of this point to the origin, that is, a point with the coordinates ( 0, 0).
my code:
(define x 3)
(define y 4)
(define (square x)(* x x))

(sqrt (+ (square x) (square y)))
-----------------------------------------------------------------------------------

1.2 The Arithmetic of Strings

字符串的拼接,像python中的 str1 + str2

> (string-append "what a " "lovely " "day" " 4 BSL")

"what a lovely day 4 BSL"

Exercise 2. Add the following two lines to the definitions area:
(define prefix "hello")
(define suffix "world")
Then use string primitives to create an expression that concatenates  prefix  and  suffix  and adds  "_"  between them. When you run this program, you will see  "hello_world"  in the interactions area.

My code

(define prefix "hello")
(define suffix "world")

(string-append prefix "_" suffix)
--------------------------------------------------------------

1.3 Mixing it Up

Exercise 3. Add the following two lines to the definitions area:

(define str "helloworld")
(define i 5)
Then create an expression using string primitives that adds  "_" at position  i. In general this means the resulting string is longer than the original one; here the expected result is  "hello_world".
Position means  i characters from the left of the string, but programmers start counting at  0. Thus, the 5 th letter in this example is  "w", because the  0th letter is  "h"Hint When you encounter such “counting problems” you may wish to add a string of digits below  str to help with counting:
(define str "helloworld")
(define ind "0123456789")
(define i 5)

My code:

(define str "helloworld")
(define i 5)

(string-append
 (substring str 0 i)
 "_"
 (substring str i))

Exercise 4. Use the same setup as in exercise 3 to create an expression that deletes the ith position from str. Clearly this expression creates a shorter string than the given one. Which values for i are legitimate?

My code:

(define str "helloworld")
(define i 5)

(string-append
 (substring str 0 i)
 (substring str (+ i 1)))

----------------------------------------------------------

1.4 The Arithmetic of Images

(require 2htdp/image)  首先导入图形库

  • circle produces a circle image from a radius, a mode string, and a color string;

  • ellipse produces an ellipse from two radii, a mode string, and a color string;

  • line produces a line from two points and a color string;

  • rectangle produces a rectangle from a width, a height, a mode string, and a color string;

  • text produces a text image from a string, a font size, and a color string; and

  • triangle produces an upward-pointing equilateral triangle from a size, a mode string, and a color string.

> (circle 10 "solid" "green")

image

> (rectangle 10 20 "solid" "blue")

image

> (star 12 "solid" "gray")

image


除了绘制图形,还能获取图形参数

  • image-width determines the width of an image in terms of pixels;

  • image-height determines the height of an image;

> (image-width (circle 10 "solid" "red"))

20

> (image-height (rectangle 10 20 "solid" "blue"))

20


(+ (image-width (circle 10 "solid" "red"))
   (image-height (rectangle 10 20 "solid" "blue")))

40

>

Exercise 6. Add the following line to the definitions area:

(define cat )

Create an expression that counts the number of pixels in the image.

my code:

(require 2htdp/image)

(define cat )

(image-height cat)
(image-width cat)

--------------------------------------------------------

1.5 The Arithmetic of Booleans 逻辑运算符

 or

> (or #true #true)

#true

> (or #true #false)

#true

> (or #false #true)

#true

> (or #false #false)

#false


and

> (and #true #true)

#true

> (and #true #false)

#false

> (and #false #true)

#false

> (and #false #false)

《如何设计程序》(How to Design Programs,缩写为HtDP) 是由马灿灿(Carl Eastlund, Matthew Flatt, Robert Bruce Findler, and Shriram Krishnamurthi)等人合著的一本计算机科学教材。该教材以函数式编程为基础,旨在教授学生如何设计高质量的程序。 《如何设计程序》(HtDP) 提供了一种结构化的方法,帮助学生按步骤设计和编写程序。其核心思想是将程序设计视为一种系统化的过程,通过分解问题、设计清晰的数据结构和定义适当的函数来解决问题。 设计程序的关键步骤如下: 1. 定义问题:明确问题的要求和目标,将问题分解为更小、更易处理的子问题。 2. 设计数据:确定程序所需的数据结构,包括如何组织和存储数据。 3. 规划函数:根据问题的需求,设计合适的函数,包括输入和输出的数据类型。 4. 开发程序:编写程序代码,实现定义的函数和数据结构,以解决问题。 5. 测试和调试:进行全面的测试,确保程序在不同情况下的正确性和鲁棒性。 6. 文档记录:撰写适当的文档,记录程序的设计和实现细节,以方便程序员和维护人员。 7. 优化和改进:根据实际应用中的反馈和需求,对程序进行优化和改进,提高性能和用户体验。 《如何设计程序》(HtDP) 强调迭代开发过程,通过不断重构和重复上述步骤,逐渐提高程序的质量和可靠性。同时,教材还提供了丰富的示例和练习,帮助学生理解和应用所学知识。 总而言之,设计程序需要明确问题、设计数据结构、规划函数、编写代码、测试调试,并进行文档记录和持续改进。《如何设计程序》(HtDP) 提供了一种系统性的方法,帮助学生掌握程序设计的基本原则和技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值