-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rkt
33 lines (29 loc) · 957 Bytes
/
main.rkt
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
#lang racket
;; lambda.occurs-free?: Symbol x LcExp -> Bool
;; usage: (lambda.occurs-free? var exp) = returns #t if the symbol var occurs free in exp
(define lambda.occurs-free?
(lambda (var exp)
(cond
[(symbol? exp) (eqv? exp var)]
[(eqv? (car exp) 'lambda) (and
(not (eqv? (caadr exp) var))
(lambda.occurs-free? var (caddr exp)))]
[else (or
(lambda.occurs-free? var (car exp))
(lambda.occurs-free? var (cadr exp)))])))
; LcExp ::= var
; ::= (lambda (var) LcExp
; ::= (LcExp LcExp)
; (lambda calculus expression)
; > (lambda.occurs-free? 'x 'x)
; #t
; > (lambda.occurs-free? 'x 'y)
; #f
; > (lambda.occurs-free? 'x '(lambda (x) (x y)))
; #f
; > (lambda.occurs-free? 'x '(lambda (y) (x y)))
; #t
; > (lambda.occurs-free? 'x '((lambda (x) x) (x y)))
; #t
; > (lambda.occurs-free? 'x '(lambda (y) (lambda (z) (x (y z)))))
; #t