Skip to content

Commit b49fc1c

Browse files
committed
EECS345 Programming Languages Concepts
0 parents  commit b49fc1c

12 files changed

+957
-0
lines changed

Interpreter, Part 2.pdf

177 KB
Binary file not shown.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# EECS345 Programming Languages Concepts
2+
# 加微信 powcoder
3+
4+
# QQ 1823890830
5+
6+
# Programming Help Add Wechat powcoder
7+
8+
# Email: powcoder@163.com
9+

abstraction.scm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; A function computing the denotational semantics for the value of an expression.
2+
; The function uses abstraction for the operand and operator locations in the expression
3+
; The abstraction makes the code easier to read and easier to modify should we need to
4+
; change expressions between infix, prefix, or postfix notation
5+
(define M_value_int
6+
(lambda (expression)
7+
(cond
8+
((number? expression) expression)
9+
((eq? '+ (operator expression)) (+ (M_value_int (operand1 expression)) (M_value_int (operand2 expression))))
10+
((eq? '- (operator expression)) (- (M_value_int (operand1 expression)) (M_value_int (operand2 expression))))
11+
((eq? '* (operator expression)) (* (M_value_int (operand1 expression)) (M_value_int (operand2 expression))))
12+
((eq? '/ (operator expression)) (quotient (M_value_int (operand1 expression)) (M_value_int (operand2 expression))))
13+
((eq? '% (operator expression)) (remainder (M_value_int (operand1 expression)) (M_value_int (operand2 expression))))
14+
(else (error 'badoperator "illegal arithmetic expression")))))
15+
16+
; Here is one way to define the abstraction function
17+
(define operator
18+
(lambda (expression)
19+
(cadr expression)
20+
21+
; And here is another equivalent way
22+
(define operand1 car)
23+
(define operand2 caddr)
24+

cpslecture1.scm

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
; Tail recursion
2+
3+
; Accumulator Passing Style
4+
; Compute the length of a list
5+
(define len
6+
(lambda (lis)
7+
(if (null? lis)
8+
0
9+
(+ 1 (len (cdr lis))))))
10+
11+
(define len-acc
12+
(lambda (lis acc)
13+
(if (null? lis)
14+
acc
15+
(len-acc (cdr lis) (+ 1 acc)))))
16+
17+
; sum the numbers in a list
18+
(define sumnumbers
19+
(lambda (lis)
20+
(cond
21+
((null? lis) 0)
22+
((number? (car lis)) (+ (car lis) (sumnumbers (cdr lis))))
23+
(else (sumnumbers (cdr lis))))))
24+
25+
(define sumnumbers-acc
26+
(lambda (lis acc)
27+
(cond
28+
((null? lis) acc)
29+
((number? (car lis)) (sumnumbers-acc (cdr lis) (+ (car lis) acc)))
30+
(else (sumnumbers-acc (cdr lis) acc)))))
31+
32+
(define sumnumbers
33+
(lambda (lis)
34+
(sumnumbers-acc lis 0)))
35+
36+
; an accumulator passing version of append
37+
(define myappend-acc
38+
(lambda (l1 l2 acc)
39+
(if (null? l1)
40+
acc
41+
(myappend-acc (cdr l1) l2 (cons (car l1) acc)))))
42+
43+
(define myappend
44+
(lambda (l1 l2)
45+
(myappend l1 l2 l2)))
46+
47+
; Continuation passing style
48+
; sum the numbers in a list
49+
(define sumnumbers-cps
50+
(lambda (lis return)
51+
(cond
52+
((null? lis) (return 0))
53+
((number? (car lis)) (sumnumbers-cps (cdr lis) (lambda (v) (return (+ (car lis) v)))))
54+
(else (sumnumbers-cps (cdr lis) return)))))
55+
56+
; factorial
57+
(define factorial
58+
(lambda (n)
59+
(if (zero? n)
60+
1
61+
(* n (factorial (- n 1))))))
62+
63+
(define factorial-cps
64+
(lambda (n return)
65+
(if (zero? n)
66+
(return 1)
67+
(factorial-cps (- n 1) (lambda (v) (return (* n v)))))))
68+
69+
; append
70+
(define myappend
71+
(lambda (l1 l2)
72+
(if (null? l1)
73+
l2
74+
(cons (car l1) (myappend (cdr l1) l2)))))
75+
76+
(define myappend-cps
77+
(lambda (l1 l2 return)
78+
(if (null? l1)
79+
(return l2)
80+
(myappend-cps (cdr l1) l2 (lambda (v) (return (cons (car l1) v)))))))
81+
82+
; removeall
83+
(define removeall
84+
(lambda (x lis)
85+
(cond
86+
((null? lis) '())
87+
((eq? x (car lis)) (removeall x (cdr lis)))
88+
(else (cons (car lis) (removeall x (cdr lis)))))))
89+
90+
(define removeall-cps
91+
(lambda (x lis return)
92+
(cond
93+
((null? lis) (return '()))
94+
((eq? x (car lis)) (removeall-cps x (cdr lis) return))
95+
(else (removeall-cps x (cdr lis) (lambda (v) (return (cons (car lis) v))))))))

cpslecture2.scm

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; append
2+
(define myappend
3+
(lambda (l1 l2)
4+
(if (null? l1)
5+
l2
6+
(cons (car l1) (myappend (cdr l1) l2)))))
7+
8+
(define myappend-cps
9+
(lambda (l1 l2 return)
10+
(if (null? l1)
11+
(return l2)
12+
(myappend-cps (cdr l1) l2 (lambda (v) (return (cons (car l1) v)))))))
13+
14+
; replaces x with y in the list (no sublists)
15+
16+
; reverse
17+
(define myreverse
18+
(lambda (lis)
19+
(if (null? lis)
20+
'()
21+
(myappend (myreverse (cdr lis)) (cons (car lis) '())))))
22+
23+
(define myreverse-cps
24+
(lambda (lis return)
25+
(if (null? lis)
26+
(return '())
27+
(myreverse-cps (cdr lis) (lambda (v) (myappend-cps v (cons (car lis) '()) return))))))
28+
29+
; count all the numbers in a list that contains lists
30+
(define countnums*
31+
(lambda (lis)
32+
(cond
33+
((null? lis) 0)
34+
((list? (car lis)) (+ (countnums* (car lis)) (countnums* (cdr lis))))
35+
((number? (car lis)) (+ 1 (countnums* (cdr lis))))
36+
(else (countnums* (cdr lis))))))
37+
38+
(define countnums*-cps
39+
(lambda (lis return)
40+
(cond
41+
((null? lis) (return 0))
42+
((list? (car lis)) (countnums*-cps (car lis) (lambda (v1) (countnum*-cps (cdr lis) (lambda (v2) (return (+ v1 v2)))))))
43+
((number? (car lis)) (countnums*-cps (cdr lis) (lambda (v) (return (+ 1 v)))))
44+
(else (countnums*-cps (cdr lis) return)))))
45+
46+
; the cps version of split. call with the initial continution
47+
; (lambda (v1 v2) (list v1 v2))
48+
(define split-cps
49+
(lambda (lis return)
50+
(cond
51+
((null? lis) (return '() '()))
52+
((null? (cdr lis)) (return lis '()))
53+
(else (split-cps (cddr lis) (lambda (v1 v2) (return (cons (car lis) v1) (cons (cadr lis) v2))))))))
54+
55+
; removeall*
56+
(define removeall*-cps
57+
(lambda (x lis return)
58+
(cond
59+
((null? lis) (return '()))
60+
((list? (car lis)) (removeall*-cps x (car lis) (lambda (v1) (removeall*-cps x (cdr lis) (lambda (v2) (return (cons v1 v2)))))))
61+
((eq? (car lis) x) (removeall*-cps x (cdr lis) return))
62+
(else (removeall*-cps x (cdr lis) (lambda (v) (return (cons (car lis) v))))))))
63+
64+
; (flatten '((a) (((b) (c)) d ((e f)))) g)) => (a b c d e f g)
65+
(define flatten-cps
66+
(lambda (lis return)
67+
(cond
68+
((null? lis) (return '()))
69+
((list? (car lis)) (flatten-cps (car lis) (lambda (v1) (flatten-cps (cdr lis) (lambda (v2) (myappend-cps v1 v2 return))))))
70+
(else (flatten-cps (cdr lis) (lambda (v) (return (cons (car lis) v))))))))

cpslecture3.scm

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
; This is needed if you do not already have call/cc defined in your Scheme implementation
2+
(define call/cc call-with-current-continuation)
3+
4+
; A multiply that uses tail recursion so we can immediately break
5+
(define multiply-cps
6+
(lambda (lis return break)
7+
(cond
8+
((null? lis) (return 1))
9+
((zero? (car lis)) (break 0))
10+
(else (multiply-cps (cdr lis) (lambda (v) (return (* (car lis) v))) break)))))
11+
12+
; a multiply that does not use tail recursion but takes in a break continuation
13+
(define multiply
14+
(lambda (lis break)
15+
(cond
16+
((null? lis) 1)
17+
((zero? (car lis)) (break 0))
18+
(else (* (car lis) (multiply (cdr lis) break))))))
19+
20+
; creates a break continuation that throws away the call stack to this point, and calls multiply with that continuation
21+
(define goodmult
22+
(lambda (lis)
23+
(call/cc
24+
(lambda (break)
25+
(multiply lis break)))))
26+
27+
;; (indexof 'x '(a b x c d e)) ==> 3
28+
;; (indexof 'x '(a b c d)) ==> -1
29+
(define indexof-helper
30+
(lambda (x lis break)
31+
(cond
32+
((null? lis) (break -1))
33+
((eq? x (car lis)) 0)
34+
(else (+ 1 (indexof x (cdr lis)))))))
35+
36+
(define indexof
37+
(lambda (x lis)
38+
(call/cc
39+
(lambda (break)
40+
(indexof-helper x lis break)))))
41+
42+
43+
44+
(define mycont (lambda (v) v))
45+
46+
; a replace all that saves the call stack at the top point into mycont so that we can reaccess that call stack anytime
47+
; we call mycont
48+
(define replaceall
49+
(lambda (x y lis)
50+
(call/cc
51+
(lambda (k)
52+
(cond
53+
((null? lis) (begin (set! mycont k) '()))
54+
((eq? x (car lis)) (cons y (replaceall x y (cdr lis))))
55+
(else (cons (car lis) (replaceall x y (cdr lis)))))))))
56+
57+
; a crazy version of append that uses the mycont call stack instead of its own!
58+
(define myappendit
59+
(lambda (l1 l2)
60+
(if (null? l1)
61+
(mycont l2)
62+
(cons (car l1) (myappendit (cdr l1) l2)))))
63+
64+
65+
; An even crazier replaceall from the 2nd lecture. Saves the point part way through the execution of replaceall*
66+
; so that if we call mycont, it embeds the input into the location of the first x in the running of replaceall*
67+
68+
(define mycont2 0)
69+
70+
(define replaceall*
71+
(lambda (x y lis)
72+
(call/cc
73+
(lambda (break)
74+
(cond
75+
((null? lis) (begin (if (eq? mycont2 0) (set! mycont2 break)) '()))
76+
((eq? x (car lis)) (cons y (replaceall* x y (cdr lis))))
77+
((list? (car lis)) (cons (replaceall* x y (car lis)) (replaceall* x y (cdr lis))))
78+
(else (cons (car lis) (replaceall* x y (cdr lis)))))))))

0 commit comments

Comments
 (0)