CS302: Paradigms of Programming Variations on the Scheme Interpreter (Cont.)
Manas Thakur
Feb-June 2020
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
Feb-June 2020
languages
2
3
4
int x = 0; int f() { return x; } int g() { int x = 1; return f(); }
5
x <- 1 f <- function(a) x + a g <- function() { x <- 2 f(0) } g()
6
higher order functions:
7
(define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) (define (sum-cubes a b) (define (cube x) (expt x 3)) (sum cube a inc b)) (define (sum-powers a b n) (define (nth-power x) (expt x n)) (sum nth-power a inc b))
8
9
(define (sum term a next n) (if (> a n) (+ (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))
10
11
(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) (define (nth-power x) (expt x n)) (sum nth-power a inc b)) (define (sum-powers a b n) (sum nth-power a inc b)) (define (product-powers a b n) (product nth-power a inc b)) (define (nth-power x) (expt x n))
12
13
In fact, we don’t need to bind the environment while creating procedure objects!
14
dynamic scoping.
scoped defaults to lexical scoping.
15
(define (foo a (dynamic b)) ( ... ) (define (foo a (dynamic b)) (+ a (dynamic-ref b))
16
(define (product-powers a b n) (product (make-exp n) a inc b)) (define (sum-powers a b n) (sum (make-exp n) a inc b)) (define make-exp (lambda (n) (lambda (x) (expt x n))))