aboutsummaryrefslogtreecommitdiff
path: root/hw06_alpha.scm
blob: 1a6cb1b41c9b308d3b7bd567a2ad5a9b9b8641a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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))))