cs302 paradigms of programming variations on the scheme
play

CS302: Paradigms of Programming Variations on the Scheme Interpreter - PowerPoint PPT Presentation

CS302: Paradigms of Programming Variations on the Scheme Interpreter (Cont.) Manas Thakur Feb-June 2020 Graduate to Post-Graduate We can use the knowledge gained to experiment with programming language technology in several ways: 1. Extend


  1. CS302: Paradigms of Programming Variations on the Scheme Interpreter (Cont.) Manas Thakur Feb-June 2020

  2. Graduate to Post-Graduate • We can use the knowledge gained to experiment with programming language technology in several ways: 1. Extend the language with new syntactic constructs 2. Explore various design choices in the space of programming languages 3. Design new programming languages themselves! 2

  3. 2a. Exploring design choices: 
 Lexical vs Dynamic Scoping 3

  4. Lexical vs dynamic scoping • What’s the value returned by g ? • Lexical scoping: 0 int x = 0; int f() { return x; } • Dynamic scoping: 1 int g() { int x = 1; return f(); } • Lexical scoping: Free variables are bound in the environment in which the procedure was defined (closure). • Examples: Pascal, Ada, Scheme, Haskell, C and family. • Dynamic scoping: Free variables are bound the most recently assigned value during program execution. • Examples: Original Lisp, Bash, LaTeX. 4

  5. Lexical vs dynamic scoping (Cont.) • What’s the value returned by g ? x <- 1 • Lexical scoping: 1 f <- function(a) x + a g <- function() { • Dynamic scoping: 2 x <- 2 f(0) } • Which language is this? g() • Homework: Find out which scoping is used in R. 5

  6. Discussion • Which one — lexical or dynamic scoping — is more natural? • Which one should be easier to implement? • Original Lisp had dynamic scoping! • Then why switch to lexical scoping in Scheme? 6

  7. Flashback • Recall the general sum procedure from the days we learnt about higher order functions: (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) • Usage: (define (sum-cubes a b) (define (cube x) (expt x 3)) Notice the 
 (sum cube a inc b)) binding • One more (sum of n th powers): (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b)) 7

  8. Why not dynamic scoping? 8

  9. Problem with dynamic scoping • Say the programmer renamed b in the procedure sum to n : (define (sum term a next n) (if (> a n) 0 (+ (term a) (sum term (next a) next n)))) (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b)) • Whoops! Problem irukke . n got bound to b 9

  10. Then why dynamic scoping? 10

  11. Classwork ( for me! ) (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b)) • Write an analogous procedure to multiply the n th powers. (define (product-powers a b n) (define (nth-power x) (expt x n)) (product nth-power a inc b)) (define (sum-powers a b n) • Can we reuse nth-power ? (sum nth-power a inc b)) (define (product-powers a b n) • Lexical scoping: No. (product nth-power a inc b)) • Dynamic scoping: Yes! (define (nth-power x) (expt x n)) 11

  12. Now comes fun! 12

  13. Make Scheme dynamically scoped • Remarkably simple: Just extend the correct environment! In fact, we don’t need to bind the environment while creating procedure objects! 13

  14. Scoping in action 14

  15. Assignment 3 Part A (7 Marks) • Extend the eval/apply interpreter to have both lexical and dynamic scoping. • A parameter may be declared to be dynamically scoped as: (define (foo a (dynamic b)) ( ... ) • A variable may be referenced dynamically as: (define (foo a (dynamic b)) (+ a (dynamic-ref b)) • Referencing a variable that wasn’t declared as dynamically scoped defaults to lexical scoping. • Interpreter implementation from textbook will be provided to you. 15

  16. Insight of the month (semester?) • How do we solve the limitation of lexical scoping? (define make-exp By being good in 
 (lambda (n) computing science :-) (lambda (x) (expt x n)))) (define (sum-powers a b n) (define (product-powers a b n) (sum (make-exp n) a inc b)) (product (make-exp n) a inc b)) • What’s the powerful feature without which you couldn’t solve this problem? • Ability to return functions as values. • In general, granting first-class citizenship to functions ! 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend