• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 商业

BUSINESS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 【用代数讲计算机科学】协约

CS in Algebra: Contracts

You’ve already seen several functions that take in 2 numbers and return a number

such as addition, subtraction, multiplication and division.
star triangle 和 circle函数
The functions star, triangle and circle

all take in a number and two strings, returning an image.

That’s where the contract comes in.

A contract is a tool to help us document all of the functions in our language.

It’s like an instruction manual for a specific function.

Let’s use the triangle function as an example.

Each contract is made up of three parts

the name the domain and the range

The name should be pretty self-explanatory

and in this case, the name of our function is triangle.

The domain of a function is a list of the types of data

the function expects in the order it expects them.

It’s important to understand that the domain is NOT the names of the inputs

like size style or color

but the data types they expect like number and string.

The final element of our contract is the range

which tells us the data type that a function returns
triangle函数有一个图片范围
The triangle function has a range of image

We use types instead of values

when we write a contract because we want to be more general.

A star could be any size

so the domain for the star specifies

that the first input could be any number value

If we think of a language as a collection of Lego pieces

the contracts are the tabs and slots

that tell us how each piece can connect.

As you encounter and eventually write more functions

you’ll want to document their contracts in the contract log

The log will serve as your manual for the language

which will be really helpful when you start to write more complex programs.

In the next stage, you’ll get a chance to write a number of different functions

and then write a contract for each one.

Make sure you also add these contracts to your log for reference later on

##### 译制信息

【B】Cod

https://www.youtube.com/watch?v=X8L259cdfns