aboutsummaryrefslogtreecommitdiff
path: root/hw06_eta.scm
blob: 991ee65352b88a366719441d290f1e41361eb0da (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
#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 '())