南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters

Interpreters
Interpreters

Tail Recursion
Tail Recursion

Tail Recursion An expression in a tail context is evaluated as the last step the function call o That means nothing is evaluated/applied after it is evaluated Function calls in a tail context are called tail calls If all recursive calls are in tail contexts,we say that function is tail recursive 0 If a language supports tail call optimization,a tail recursive function will only ever open a constant number of frames
Tail Recursion ● An expression in a tail context is evaluated as the last step the function call ○ That means nothing is evaluated/applied after it is evaluated ● Function calls in a tail context are called tail calls ● If all recursive calls are in tail contexts, we say that function is tail recursive ○ If a language supports tail call optimization, a tail recursive function will only ever open a constant number of frames

Writing Tail Recursive Functions (Method I) 1)Identify recursive calls that are not in a tail context.Tail contexts are: o The last body subexpression in a lambda (a function) o The consequent and alternative in a tail context if o All non-predicate sub-expressions in a tail context cond o The last sub-expression in a tail context and,or,begin,or let 2)Create a helper function with arguments to accumulate the computation that prevents it from being tail recursive
Writing Tail Recursive Functions (Method I) 1) Identify recursive calls that are not in a tail context. Tail contexts are: ○ The last body subexpression in a lambda (a function) ○ The consequent and alternative in a tail context if ○ All non-predicate sub-expressions in a tail context cond ○ The last sub-expression in a tail context and, or, begin, or let 2) Create a helper function with arguments to accumulate the computation that prevents it from being tail recursive

Demo Example:Length of Linked List Goal:Write a function that takes in a list and returns the length of the list.Make sure it is tail recursive. (define (length 1st) (define (length-tail lst) (if(nu11?1st) 0 (1 (1ength (cdr 1st))))) scm> (1 ength‘() 0 scm>(1 ength‘(12(34) 3
Example: Length of Linked List Goal: Write a function that takes in a list and returns the length of the list. Make sure it is tail recursive. (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst))))) scm> (length ‘()) 0 scm> (length ‘(1 2 (3 4)) 3 (define (length-tail lst) ) Demo

Interpretation
Interpretation

Translation Problem: Computers can only understand one language,binary (0s and 1s) Humans can't really write a program using only 0s and 1s (not quickly anyways) Solution: Programming languages Languages like Python,Java,C,etc are translated to 0s and 1s This translation step comes in a couple forms: Compiled (pre-translated)_-translate all at once and run later iInterpreted (translated_on-the-fly)-translate while the program is running We'11 focus on interpreted languages
Translation Problem: Computers can only understand one language, binary (0s and 1s) Humans can’t really write a program using only 0s and 1s (not quickly anyways) Solution: Programming languages Languages like Python, Java, C, etc are translated to 0s and 1s This translation step comes in a couple forms: Compiled (pre-translated) - translate all at once and run later Interpreted (translated on-the-fly) - translate while the program is running We’ll focus on interpreted languages

Interpreters An interpreter does 3 things: Reads input from user in a specific programming language Translates input to be computer readable and evaluates the result Prints the result for the user There are two languages involved: Implemented language:this is the language the user types in Implementation language:this is the language interpreter is implemented in Implemented Language is translated into the Implementation Language
Interpreters An interpreter does 3 things: Reads input from user in a specific programming language Translates input to be computer readable and evaluates the result Prints the result for the user There are two languages involved: Implemented language: this is the language the user types in Implementation language: this is the language interpreter is implemented in Implemented Language is translated into the Implementation Language

Read-Eval-Print Loop (REPL) input expression value output Read Eval Print 。 string string loop while True: exp read() val eval(exp) print(val)
Interpreter input string Read-Eval-Print Loop (REPL) Read Eval Print expression value loop output string while True: exp = read() val = eval(exp) print(val)

Read
Read
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)24-SQL-II.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)25-Conclusion, and Final Exam Review.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)01 绪论 Introduction(主讲:曹洋).pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)02 傅里叶分析与卷积 Fourier Analysis and Convolution.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)03 数字图像处理基础 Basics of Digital Image Processing.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)04 图像模型 Basics of Image.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)05 空域滤波 Spatial Filtering.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)06 小波变换 Wavelet Analysis.pptx
- CodeIgniter 中国开发者社区:CodeIgniter4 中文手册(版本 4.0.0).pdf
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)07 图像复原 Image Restoration.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)08 自适应滤波 Adaptive Filter.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)要点复习 Review(主讲:曹洋).pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)09 图像压缩 Image Compression.pptx
- 中国科学技术大学:《计算机视觉》课程教学资源(参考论文)Tour Into the Picture_Using a Spidery Mesh Interface to Make Animation from a Single Image.pdf
- 中国科学技术大学:《计算机视觉》课程教学资源(参考论文)3D photography on your desk.pdf
- 中国科学技术大学:《计算机视觉》课程教学资源(PPT课件讲稿)第一章 绪论 Computer Vison(主讲:曹洋).ppt
- 中国科学技术大学:《计算机视觉》课程教学资源(PPT课件讲稿)第二章 视觉的基本知识 第一节 人类生理视觉系统.ppt