diff options
author | Ben Burwell <bburwell1@gmail.com> | 2013-04-11 00:03:50 -0400 |
---|---|---|
committer | Ben Burwell <bburwell1@gmail.com> | 2013-04-11 00:03:50 -0400 |
commit | 5b05b64a2a658c0f7d4eb5b09fa342c7375a776e (patch) | |
tree | bad4537081da8b969084cff6880e36418f13a97d /hw06 |
Init
Diffstat (limited to 'hw06')
-rw-r--r-- | hw06/hw06_alpha.scm | 52 | ||||
-rw-r--r-- | hw06/hw06_combinators.scm | 21 | ||||
-rw-r--r-- | hw06/hw06_eta.scm | 50 |
3 files changed, 123 insertions, 0 deletions
diff --git a/hw06/hw06_alpha.scm b/hw06/hw06_alpha.scm new file mode 100644 index 0000000..1a6cb1b --- /dev/null +++ b/hw06/hw06_alpha.scm @@ -0,0 +1,52 @@ +#lang eopl + +; Ben Burwell +; Dr. Kussmaul +; CSI-310 :: Programming Languages +; Homework 6: Alpha conversions + +;; ========== PROCEDURE ========== +;; NAME: alpha-conv +;; DESC: returns the alpha conversion of an expression if +;; it exists. If there is no alpha conversion, the +;; expression itself is returned. +(define alpha-conv + (lambda (from to exp) + (if + (list? exp) + (if + (equal? (length exp) 3) + (if + (and + (equal? (car exp) 'lambda) + (list? (cadr exp)) + (equal? (length (cadr exp)) 1) + (list? (caddr exp)) + ) + (list 'lambda (list to) (list-repl from to (caddr exp))) + exp) + exp) + exp))) + +;; ========== PROCEDURE ========== +;; NAME: list-repl +;; DESC: a helper function for the alpha conversion +;; replaces all occurrences of from with to in +;; exp unless there is a nested lambda function, +;; in which case it remains intact. +(define list-repl + (lambda (from to exp) + (cond + [ (equal? (length exp) 0) '() ] + [ (equal? (car exp) from) (cons to (list-repl from to (cdr exp))) ] + [ (and (list? (car exp)) (equal? (caar exp) 'lambda)) (cons (car exp) (list-repl from to (cdr exp))) ] + [ (list? (car exp)) (cons (list-repl from to (car exp)) (list-repl from to (cdr exp))) ] + [ else (cons (car exp) (list-repl from to (cdr exp))) ] + ))) + +;; ========== TEST CODE ========== +(alpha-conv 'x 'y '(x z)) +(alpha-conv 'x 'y '((lambda ( x ) (+ x 5)) 2)) +(alpha-conv 'x 'y '(lambda ( x ) (x z x))) +(alpha-conv 'x 'y '(lambda ( y ) (y z y))) +(alpha-conv 'x 'y '(lambda ( x ) (+ x ((lambda (x) (* 2 x)) 7))))
\ No newline at end of file diff --git a/hw06/hw06_combinators.scm b/hw06/hw06_combinators.scm new file mode 100644 index 0000000..0c3fdaf --- /dev/null +++ b/hw06/hw06_combinators.scm @@ -0,0 +1,21 @@ +#lang eopl + +; Ben Burwell +; Dr. Kussmaul +; CSI-310 Programming Languages +; HW06 :: Combinators + +; A. evaluate boolean logic expression +(or F (and T (not F))) +(or F (and T T)) +(or F T) +T + +; B. compute 2+1 using Church numerals +(increment 2) + +((λ (n) (λ (f) (λ (x) (f ((n f) x))))) (λ (f) (λ (x) (f (f x))))) +( λ (f) (λ (x) (f (((λ (f) (λ (x) (f (f x)))) f) x)))) +( λ (f) (λ (x) (f ( (λ (x) (f (f x))) x)))) +( λ (f) (λ (x) (f (f (f x))))) +(λ (f) (λ (x) (f (f (f x)))))
\ No newline at end of file diff --git a/hw06/hw06_eta.scm b/hw06/hw06_eta.scm new file mode 100644 index 0000000..991ee65 --- /dev/null +++ b/hw06/hw06_eta.scm @@ -0,0 +1,50 @@ +#lang eopl + +; Ben Burwell +; Dr. Kussmaul +; CSI-310 :: Programming Languages +; Homework 6: Eta Conversion + +;; ========== PROCEDURE ========== +;; NAME: eta-conv +;; DESC: takes a lambda expression and returns +;; its eta conversion. If the expression +;; does not have an eta-conversion, it +;; returns the expression itself. + +(define eta-conv + (lambda (lst) + (if ;; check that the parameter is a list of length 3 + (and + (list? lst) + (equal? (length lst) 3) + ) + (if ;; it is a list with the correct length, check + ;; that it has an eta-conversion + (and + (equal? (car lst) 'lambda) + (list? (cadr lst)) + (equal? (length (cadr lst)) 1) + (list? (caddr lst)) + (equal? (length (caddr lst)) 2) + (equal? (caadr lst) (cadr (caddr lst))) + ) + + ;; it does, so return the conversion + (car (caddr lst)) + + ;; it doesn't, so return the expression + lst) + + ;; not a parseable expression, return it + lst))) + +;; ========== TEST CODE ========== +;; should return a: +(eta-conv '(lambda (x) (a x))) + +;; should return (lambda (x) (x a)) +(eta-conv '(lambda (x) (x a))) + +;; should return () +(eta-conv '()) |