6.00.1x Introduction to computation
6.00 Introduction to Computer Science
and Programming
• Goal:
–Become skillful at making a computer do what
you want it to do
– Learn computational modes of thinking
– Master the art of computational problem solving
What does a computer do?
• Fundamentally a computer:
– Performs calculations
– Remembers the results
• What calculations?
– Built in primitives
– Creating our own methods of calculating
(最关键的事情是如何创造我们自己的计算方法、如何获取计算机一样的思考,也就是计算思维,然后按照这样的方式思考,以便计计算机能够提取它)
(计算思维模式意味着一切都可以看做是一个涉及到数字和公式的数学问题。)
Computational problem solving
• What is computation?
– What is knowledge?
– Declarative knowledge(陈述性知识)
• Statements of fact
– Imperative knowledge(程序性知识)
• “how to” methods or recipes
Declarative knowledge
• “The square root of a number x is a number y such that y*y = x”
•Can you use this to find the square root of a particular instance of x?
(答案是不能,所以虽然陈述性知识是许多知识基石,但他不是我们所需要的)
Imperative knowledge
• Here is a “recipe” for deducing a square root Of a number x – attributed to Heron of
Alexandria in the first century AD
• Start with a guess, called g?
• If g*g is close enough to x, stop and say that g is the answer
• Otherwise make a new guess, by averaging g and x/g ?
• Using this new guess, repeat the process until we get close enough
(这就是程序性知识,寻找东西的方法)
How do we transform a recipe in a mechanical process?
• Build a machine to compute square roots
–Fixed Program Computers(固定程序计算机)
• Calculator
• Atanasoff and Berry’s (1941) computer for systems of linear equations
•Alan Turing’s (1940’s) bombe – decode Enigma codes
• Use a machine that stores and manipulates Instructions(能存储和操作命令)
– Stored Program Computer
Stored program computer
• Sequence of instructions (program) stored inside computer
– Built from predefined set of primitive instructions
• Arithmetic and logic
• Simple tests
• Moving data
• Special program (interpreter) executes(执行) eachinstruction in order
A basic machine architecture
八根箭头代表着什么?
代码在内存中创造一套指令,这里有一个控制单元让我们按照指令顺序来执行,让数据通过ALU再回到内存中,有时还会执行测试指令跳转到代码的其他位置,它会一直不停做这些,直到OUTPUT最后果。
(Turing showed 6种原语就能计算任何计算,还能写一些代码序列创造新的原语,然后加入原语集中供整个系统使用。原语集是预先设定好的,在ALU中让计算机工作的组件)
Where can things go wrong?
• Syntactic errors(句法的错误)
– Common but easily caught by computer
• Static semantic errors(静态语义错误)
– Some languages check carefully before running,
others check while interpreting the program
– If not caught, behavior of program unpredictable
• Programs don’t have semantic errors, but meaning may not be what was intended(意思 不明确)
– stops running
– Runs forever
– Produces an answer, but not programmer’s intent(意图)
Our goal
• Learn the syntax and semantics of a programming language
• Learn how to use those elements to translate “recipes” for solving a problem into a form that the computer can use to do the work for us
• Computational modes of thought enable us to use a suite of methods to solve problems
Options for programming languages
•低级语言指令与control unit指令相似,能做到:
– Move data from one location to another
– Execute a simple ALU operation
– Jump to new point in sequence based on compare
• Checker confirms syntax static semantics correct
• Interpreter只能按序执行instruction
高级语言能执行更加抽象的术语:
–编译型语言多了一个编译器,将检查过的指令序列转化成我们需要的基本指令。
–解释型语言中有一种程序能将源代码转化成中间数据然后执行,不需要解释器,但每次只能对一条指 令进行转化和执行。
–编译型语言因为提前编译完成所有指令所以跑起来更快,但得在编译后的指令里去寻找实际指令中的 错误,十分困难。而解释型语言速度会慢点,但在准确知道错误发生时在代码的什么地方。
Iterative code
• Branching structures (conditionals) let us jump to different pieces of code based on a test
– Programs are constant time
• Looping structures (e.g., while) let us repeat pieces of code until a condition is satisfied
– Programs now take time that depends on values of variables, as well as length of program
Loop characteristics(特征)
• Need a loop variable
– it is Initialized outside loop
– it Changes within loop
– Test for termination(结束) depends on variable
• Useful to think:
–性质是将一组程序变量映射到整数
定义一个函数使用另一个函数时的发现
•def程序遇到return时结束
•在环境框架中函数名和相应程序bond变量和值bond
•每一次调用新的函数都会创建一个新的框架,其中变量会为了调用进行局部bond
递归
•首先”递归步骤”,怎样才能使用已知的操作方法,将问题转化成一个更简单的版本。
•第二步,我们不能一直简化却解不开,所以要把计算它变得我们可以直接解决,这叫“基线条件”。
•也可以直接考虑“递归步骤”是什么,“基线条件”是什么
Debugging
•black box:找到一系列输入样本分类和预期输出
•white box:
—找到分支处,穷举路径并考虑边界情况;
—找到循环处,抽取循环没有未完成,完成了一次,完成了若干次三个案例使问题暴露
•conducting测试:首先单元测试,测试每个模块是否运行良好,抓算法编写错误;然后集成测试,整体的系统是否工作正常,抓模块交互错误。一直反复两个步骤。(基于黑盒和白盒)
•驱动测试:Exp print二分法检索发现变量和他们的值
OOP
•只考虑程序本身行为Everything is an object with type
•将对象看作被封装的数据抽象
–将内部表示和访问接口分离
–接口定义了行为,隐藏了里面发生的事
•它会让我们聚焦对象,将它们作为基础的,原始的实体,然后思考我们如何操作它即可
•如果我们有了对象,我们需要为它创建实例,就是特定版本
•Advantages:
–分而治之开发:每种类型的对象可以独立调试,而且降低了复杂度
–代码编写更加容易,因为每建立一个新类我们会创建与之相关联的方法
–定义一个类就会在内存中创建一个保存内部信息的新环境
–能产生对象的特化,继承其基本行为,但也定义了新的行为
What do computer scientists do?
• They think computationally – Abstractions, algorithms, automated execution 他们运用抽 象将问题分解成许多片段,然后封装起来不用担心里面细节,只需考虑抽象的契约就可以了,给值得到 反回值;他们运用算法,通过一系列按顺序排列的步骤生成问题答案;把实际运行这一系列步骤的工 作交给自动执行的机器—求值器(处理问题的方式跟物理学家、生物学家、化学家,经济学家一样、 但和人文学家处理问题的方式却不同)
• Computational thinking will be a fundamental skill used by everyone
• Just like the three r’s: reading, ‘riting, and ‘rithmetic at last century and Now帮你对付 任何问题
Computational Thinking: the Process
• determine or invent useful abstractions – Suppressing details, formulating interfaces
给定一个问题要讲清楚,要找出问题的关键元素,以特殊方法确定它们—抽象表示我要描述的一个 组件,组件内部我或他人会构建细节来执行组件应该做的事。但抽象表示我可以通过压缩细节掩盖信 息表示我知道这个东西怎么工作.
这个步骤可能运行好几次,第一次解决问题时,我可能构建一个抽象集合,但应用时发现没有写 对,我可能要么添加另一个抽象,要么重写某个特定抽象的接口.
• Formulate solution to a problem and make a computational experiment(计算实验)
• Design and construct a sufficiently efficient implementation of experiment
不必花太多时间创建它可能造成不必要的损失,有许多不同的方式推出效率等级
• Validate experimental setup (i.e., debug it)
测试它的每一个单元,测试它的整体,确保满足不同边界条件或特殊用例,确保整个系统可以工作
• Run experiment
• Evaluate(评估) results of experiment保证得到的结果有意义
• Repeat as needed通过测试可能暴露出那些与我预期相驳论的地方,可能需要循环好几次才能重新 定义或设计计算试验中要实际应用什么
The three A’ s of computational thinking
• Abstraction
– 用好方法来设计抽象,这就要理解问题中的边界是什么,它将哪些东西分开了
– 操作可能涉及多层抽象
– clear the relationships the between layers(抽象)
• Automation
– Think in mechanizing our abstractions
– Mechanization is possible
• Because we have precise and exacting notations and models我们有精确严格的标记和模型
• There is some “machine” that can interpret our notations (in the computer)
• Algorithms
– A Language for describing automated processes
– 也可以用来构造抽象细节
Where have you been?
• Four major topics (and a language)
1. Learning a language for expressing computations – Python
2. Learning about the process of writing and debugging a program – Be systematic
3. Learning to estimate computational complexity
4. Learning about the process of moving from a problem statement to a computational formulation of a method for solving the problem – Use abstraction
5. Learning a basic set of recipes – Algorithms
Why Python?
• Relatively easy to learn and use – Simple syntax
– Interpretive, which makes debugging easier
– Don’t have to worry about managing memory
• Modern
– Supports currently stylish mode of programming, object-oriented
• Increasingly popular
– Used in an increasing number of subjects at MIT and elsewhere
– Increasing use in industry
– Large and ever growing set of libraries
我只想建议你,希望你拥有的第一件宝物就是一辈子用计算思维进行思考
Where are you headed?
• There are other CS courses for which you are now prepared – Second part of 6.00
– optimization, modeling, simulation
– Introductory algorithms and data structures
– Introduction to artificial intelligence
– Software engineering
– Computer architecture
************************get箭袋里的新剑#**************************