How to Design Programs, Second Edition——Chapter 0.序 (笔记)

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





序言:如何编程

先下载安装 DrRacket

language选项,选择Beginning Student, 简称BSL。


输入 (+ 1 1) 点击“Run”



(+ 2 2)

(* 3 3)

(- 4 2)

(/ 6 2)

试试这些基础运算。

> (+ 2 (+ (* 3 3) 4))

15

> (+ 2 (+ (* 3 (/ 12 4)) 4))

15

> (+ (* 5 5) (+ (* 3 (/ 12 4)) 4))

38

一些嵌套运

> (+ (1) (2))

function call:expected a function after the open parenthesis, found a number

注意括号就是运算符

> (+ 1 2 3 4 5 6 7 8 9 0)

45

> (* 1 2 3 4 5 6 7 8 9 0)

0

累加,累

> "hello world"

"hello world"

字符

> (string-append "hello" "world")

"helloworld"

> (string-append "hello " "world")

"hello world"

字符串拼


逻辑运算

> (and #true #true)

#true

> (and #true #false)

#false

> (or #true #false)

#true

> (or #false #false)

#false

> (not #false)

#true


> (> 10 9)

#true

> (< -1 0)

#true

> (= 42 9)

#false


(and (or (= (string-length "hello world")
            (string->number "11"))
         (string=? "hello world" "good morning"))
     (>= (+ (string-length "hello world") 60) 80))

(require 2htdp/image)是导入库文件

除了插入外来图片,也可以利用程序自己画图

> (circle 10 "solid" "red")

image

> (rectangle 30 20 "outline" "blue")

image


将圆形和长方形叠在一起

(overlay (circle 5 "solid" "red")
           (rectangle 20 20 "solid" "blue"))

image

> (overlay (rectangle 20 20 "solid" "blue")
           (circle 5 "solid" "red"))

image


overlay 就像 string-append和+

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

10

> (image-width
    (overlay (rectangle 20 20 "solid" "blue")
             (circle 5 "solid" "red")))

20

image-width就是测量图形的宽度


(place-image (circle 5 "solid" "green")
             50 80
             (empty-scene 100 100))


empty-scene 创建 100 * 100的区域(左上角坐标为0,0),place-inage 放置图形

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

Input and Output 输入输出


?号部分是多少?


第4张图里小红点的位置应该在哪里?

这里引出函数的概念 y = x * x

函数define 

(define (y x) (* x x))

(define (y x) (* x x))
 
(y 1)
(y 2)
(y 3)
(y 4)
(y 5)

函数的定义格式:

  1. 定义 (define (FunctionName InputName) BodyExpression)
  2. 调用 (FunctionName ArgumentExpression)

> (place-image image 50 23 (empty-scene 100 60))

image





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

Many Ways to Compute


(define (sign x)
  (cond
    [(> x 0) 1]
    [(= x 0) 0]
    [(< x 0) -1]))

Cond 条件判断语句格式

(cond
  [ConditionExpression1 ResultExpression1]
  [ConditionExpression2 ResultExpression2]
  ...
  [ConditionExpressionN ResultExpressionN])

猜你喜欢

转载自blog.csdn.net/seeleday/article/details/80615773