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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值