Introduction to Programming.ppt
《Introduction to Programming.ppt》由会员分享,可在线阅读,更多相关《Introduction to Programming.ppt(44页珍藏版)》请在麦多课文档分享上搜索。
1、S. Haridi and P. Van Roy,1,Introduction to Programming,Seif Haridi KTHPeter Van Roy UCL,S. Haridi and P. Van Roy,2,Introduction,An introduction to programming concepts Simple calculator Declarative variables Functions Structured data (example: lists) Functions over lists Correctness and complexity L
2、azy functions Concurrency and dataflow State, objects, and classes Nondeterminism and atomicity,S. Haridi and P. Van Roy,3,A calculator,Use the system as a calculator Oz Browse 9999*9999,S. Haridi and P. Van Roy,4,Variables,Variables are short-cuts for values, they cannot be assigned more than onced
3、eclareV = 9999*9999Browse V*VVariable identifiers: is what you type Store variable: is part of the memory system The declare statement creates a store variable and assigns its memory address to the identifier V in the environment,S. Haridi and P. Van Roy,5,Functions,Compute the factorial function: S
4、tart with the mathematical definitiondeclarefun Fact Nif N=0 then 1 else N*Fact N-1 endend Fact is declared in the environment Try large factorial Browse Fact 100,S. Haridi and P. Van Roy,6,Composing functions,Combinations of r items taken from n. The number of subsets of size r taken from a set of
5、size n,declarefun Comb N RFact N div (Fact R*Fact N-R)end,Example of functional abstraction,Comb,Fact,Fact,Fact,S. Haridi and P. Van Roy,7,Structured data (lists),Calculate Pascal triangle Write a function that calculates the nth row as one structured value A list is a sequence of elements:1 4 6 4 1
6、 The empty list is written nil Lists are created by means of ”|” (cons)declareH=1T = 2 3 4 5Browse H|T % This will show 1 2 3 4 5,S. Haridi and P. Van Roy,8,Lists (2),Taking lists apart (selecting components) A cons has two components a head, and taildeclare L = 5 6 7 8L.1 gives 5L.2 give 6 7 8,|,|,
7、|,6,7,8,nil,S. Haridi and P. Van Roy,9,Pattern matching,Another way to take a list apart is by use of pattern matching with a case instruction case L of H|T then Browse H Browse T end,S. Haridi and P. Van Roy,10,Functions over lists,Compute the function Pascal N Takes an integer N, and returns the N
8、th row of a Pascal triangle as a list For row 1, the result is 1 For row N, shift to left row N-1 and shift to the right row N-1 Align and add the shifted rows element-wise to get row N,1,1,1,1,2,1,1,3,3,1,1,4,6,4,1,(0),(0),0 1 3 3 11 3 3 1 0,Shift right,Shift left,S. Haridi and P. Van Roy,11,Functi
9、ons over lists (2),declare fun Pascal Nif N=1 then 1elseAddListShiftLeft Pascal N-1ShiftRight Pascal N-1end end,AddList,ShiftLeft,ShiftRight,Pascal N-1,Pascal N-1,Pascal N,S. Haridi and P. Van Roy,12,Functions over lists (3),fun ShiftLeft Lcase L of H|T thenH|ShiftLeft Telse 0end endfun ShiftRight L
10、 0|L end,fun AddList L1 L2case L1 of H1|T1 thencase L2 of H2|T2 thenH1+H2|AddList T1 T2endelse nil end end,S. Haridi and P. Van Roy,13,Top-down program development,Understand how to solve the problem by hand Try to solve the task by decomposing it to simpler tasks Devise the main function (main task
11、) in terms of suitable auxiliary functions (subtasks) that simplifies the solution (ShiftLeft, ShiftRight and AddList) Complete the solution by writing the auxiliary functions,S. Haridi and P. Van Roy,14,Is your program correct?,”A program is correct when it does what we would like it to do” In gene
12、ral we need to reason about the program: Semantics for the language: a precise model of the operations of the programming language Program specification: a definition of the output in terms of the input (usually a mathematical function or relation) Use mathematical techniques to reason about the pro
13、gram, using programming language semantics,S. Haridi and P. Van Roy,15,Mathematical induction,Select one or more input to the function Show the program is correct for the simple cases (base case) Show that if the program is correct for a given case, it is then correct for the next case. For integers
14、 base case is either 0 or 1, and for any integer n the next case is n+1 For lists the base case is nil, or a list with one or few elements, and for any list T the next case H|T,S. Haridi and P. Van Roy,16,Correctness of factorial,fun Fact Nif N=0 then 1 else N*Fact N-1 end endBase Case: Fact 0 retur
15、ns 1 (N1), N*Fact N-1 assume Fact N-1 is correct, from the spec we see the Fact N is N*Fact N-1 More techniques to come !,S. Haridi and P. Van Roy,17,Complexity,Pascal runs very slow, try Pascal 24 Pascal 20 calls: Pascal 19 twice, Pascal 18 four times, Pascal 17 eight times, ., Pascal 1 219 times E
16、xecution time of a program up to a constant factor is called programs time complexity. Time complexity of Pascal N is proportional to 2N (exponential) Programs with exponential time complexity are impractical,declare fun Pascal Nif N=1 then 1elseAddListShiftLeft Pascal N-1ShiftRight Pascal N-1end en
17、d,S. Haridi and P. Van Roy,18,fun FastPascal Nif N=1 then 1elselocal L inL=FastPascal N-1AddList ShiftLeft L ShiftRight Lendend end,Faster Pascal,Introduce a local variable L Compute FastPascal N-1 only once Try with 30 rows. FastPascal is called N times, each time a list on the average of size N/2
18、is processed The time complexity is proportional to N2 (polynomial) Low order polynomial programs are practical.,S. Haridi and P. Van Roy,19,Lazy evaluation,The functions written so far are evaluated eagerly (as soon as they are called) Another way is lazy evaluation where a computation is done only
19、 when the results is needed,declare fun lazy Ints NN|Ints N+1 end,Calculates the infinite list: 0 | 1 | 2 | 3 | .,S. Haridi and P. Van Roy,20,Lazy evaluation (2),Write a function that computes as many rows of Pascals triangle as needed We do not know how many beforehand A function is lazy if it is e
20、valuated only when its result is needed The function PascalList is evaluated when needed,fun lazy PascalList RowRow | PascalList AddList ShiftLeft RowShiftRight Row end,S. Haridi and P. Van Roy,21,Lazy evaluation (3),Lazy evaluation will avoid redoing work if you decide first you need the 10th row a
21、nd later the 11th row The function continues where it left off,declare L = PascalList 1 Browse L Browse L.1 Browse L.2.1,L 1 1 1,S. Haridi and P. Van Roy,22,Higher-order programming,Assume we want to write another Pascal function which instead of adding numbers performs exclusive-or on them It calcu
22、lates for each number whether it is odd or even (parity) Either write a new function each time we need a new operation, or write one generic function that takes an operation (another function) as argument The ability to pass functions as argument, or return a function as result is called higher-orde
23、r programming Higher-order programming is an aid to build generic abstractions,S. Haridi and P. Van Roy,23,Variations of Pascal,Compute the parity Pascal triangle,1,1,1,1,2,1,1,3,3,1,1,4,6,4,1,1,1,1,1,0,1,1,1,1,1,1,0,0,0,1,fun Xor X Y if X=Y then 0 else 1 end end,S. Haridi and P. Van Roy,24,Higher-o
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTOPROGRAMMINGPPT
