Original Grammar for Algebra Programs Change #1: Move ...

Original Grammar for Algebra Programs

::= * ::= () = ::= ( + )

::= ( - ) ::= () ::= | ::= a variable name: f, x, y, z, ... ::= a number: 1, 42, 17, ...

Change step-by-step to Scheme

Change #1: Move Parens and Add "define"

::= * ::= (define ( ) ) ::= (+ )

::= (- ) ::= ( ) ::= | ::= a variable name: f, x, y, z, ... ::= a number: 1, 42, 17, ...

+ and - moved to initial position

Change #1: Move Parens and Add "define"

::= * ::= (define ( ) ) ::= (+ )

::= (- ) ::= ( ) ::= | ::= a variable name: f, x, y, z, ... ::= a number: 1, 42, 17, ...

Open parenthesis for function call moved to before the function name

Change #1: Move Parens and Add "define"

::= * ::= (define ( ) ) ::= (+ )

::= (- ) ::= ( ) ::= | ::= a variable name: f, x, y, z, ... ::= a number: 1, 42, 17, ...

Definition use the define keyword, and parenthesis moved

1-6

Change #1: Move Parens and Add "define"

::= * ::= (define ( ) ) ::= (+ )

::= (- ) ::= ( ) ::= |

::= a variable name: f, x, y, z, ... ::= a number: 1, 42, 17, ...

f(x) = (x + 1) f((2 + 3))

(define (f x) (+ x 1)) (f (+ 2 3))

Change #2: Generalize + and -, Add Primitives

::= ... ::= (+ *) ::= (- +) ::= ( *) ::= (/ +) ::= (modulo ) ::= (expt ) ::= ...

(+) 0

Change #2: Generalize + and -, Add Primitives

::= ... ::= (+ *) ::= (- +) ::= ( *) ::= (/ +) ::= (modulo ) ::= (expt ) ::= ... (+ 1 2 3 7) 13

Change #2: Generalize + and -, Add Primitives

::= ... ::= (+ *) ::= (- +) ::= ( *) ::= (/ +) ::= (modulo ) ::= (expt ) ::= ... (- 1) -1

7-11

Change #2: Generalize + and -, Add Primitives

::= ... ::= (+ *) ::= (- +) ::= ( *) ::= (/ +) ::= (modulo ) ::= (expt ) ::= ...

() 1

Change #2: Generalize + and -, Add Primitives

::= ... ::= (+ *) ::= (- +) ::= ( *) ::= (/ +) ::= (modulo ) ::= (expt ) ::= ...

(/ 2) 1/2

Change #2: Generalize + and -, Add Primitives

::= ... ::= (+ *) ::= (- +) ::= ( *) ::= (/ +) ::= (modulo ) ::= (expt ) ::= ...

(expt 13 20) 19004963774880799438801

Change #3: Generalize Defined Functions

::= (define ( *) ) ::= ...

::= ( *)

(define (f a b) (+ a b)) (f 1 2)

(define (f a b) (+ a b)) 3

12-16

Change #3: Generalize Defined Functions

::= (define ( *) ) ::= ...

::= ( *)

... (define (0 1...k) 0) ... ... (0 1...k) ...

... (define (0 1...k) 0) ... ... 3 ... where 3 is 0 with i replaced by i

Change #4: Add Booleans

::= ... ::= ::= (and *) ::= (or *) ::= (zero? ) ::= ...

::= #f ::= #t

(zero? 1) #f

Change #4: Add Booleans

::= ... ::= ::= (and *) ::= (or *) ::= (zero? ) ::= ...

::= #f ::= #t

(and #t #f) #f

Change #4: Add Booleans

::= ... ::= ::= (and *) ::= (or *) ::= (zero? ) ::= ...

::= #f ::= #t

(zero? 0) #t

17-21

Change #5: Add Symbols

::= ... ::= ::= (eq? ) ::= ...

::= '

(eq? 'a 'a) #t

Change #6: Add Conditionals

::= ...

::= (cond *)

::= [ ]

::= [else ]

(cond [#t 1]) 1

Change #5: Add Symbols

::= ... ::= ::= (eq? ) ::= ...

::= '

(eq? 'a 'b) #f

Change #6: Add Conditionals

::= ...

::= (cond *)

::= [ ]

::= [else ]

(cond [#f 1] [#t 2]) (cond [#t 2]) (cond [#t 2]) 2

22-28

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download