-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add free variable finder file (fv.rkt)
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#lang racket | ||
(require "ast.rkt") | ||
(provide fv) | ||
|
||
;; Expr -> [Listof Id] | ||
;; List all of the free variables in e | ||
(define (fv e) | ||
(remove-duplicates (fv* e))) | ||
|
||
(define (fv* e) | ||
(match e | ||
[(Var x) (list x)] | ||
[(Prim1 p e) (fv* e)] | ||
[(Prim2 p e1 e2) (append (fv* e1) (fv* e2))] | ||
[(Prim3 p e1 e2 e3) (append (fv* e1) (fv* e2) (fv* e3))] | ||
[(If e1 e2 e3) (append (fv* e1) (fv* e2) (fv* e3))] | ||
[(Begin e1 e2) (append (fv* e1) (fv* e2))] | ||
[(Let x e1 e2) (append (fv* e1) (remq* (list x) (fv* e2)))] | ||
[(App e1 es) (append (fv* e1) (append-map fv* es))] | ||
[(Lam f xs e) (remq* xs (fv* e))] | ||
[_ '()])) |