;; ------------------------------------------------------------------------ ;; | FILE : syntax-procs.rkt | ;; | AUTHOR : Eugene Wallingford | ;; | CREATION DATE : 2025/03/03 | ;; | DESCRIPTION : These functions implement syntax procedures for | ;; | a simple language grammar consisting only of | ;; | variable references, lambda expressions, and | ;; | function applications. | ;; ------------------------------------------------------------------------ #lang racket (provide exp? varref? make-varref lambda? make-lambda lambda->param lambda->body app? make-app app->proc app->arg) ;; ------------------------------------------------------------------------- ;; This code works with the following grammar: ;; ;; ::= ;; | ( lambda ( ) ) ;; | ( ) ;; ------------------------------------------------------------------------- ;; ------------------------------------------------------------------------- ;; type predicate for language expressions (define (exp? exp) (or (varref? exp) (lambda? exp) (app? exp))) ;; ------------------------------------------------------------------------- ;; varrefs (define varref? symbol?) (define make-varref identity) ;; ------------------------------------------------------------------------- ;; lambda expressions (define (lambda? exp) (and (list? exp) (= (length exp) 3) ; ---------------------------------- lambda keyword (eq? (first exp) 'lambda) ; ---------------------------------- parameter list (list? (second exp)) (= 1 (length (second exp))) (symbol? (first (second exp))) ; ---------------------------------- body (exp? (third exp)))) (define lambda->param caadr) ; (first (second exp)) (define lambda->body third) (define (make-lambda parameter body) (list 'lambda (list parameter) body)) ;; ------------------------------------------------------------------------- ;; function applications (define (app? exp) (and (list? exp) (= (length exp) 2) (exp? (first exp)) (exp? (second exp)))) (define app->proc first) (define app->arg second) (define (make-app fn arg) (list fn arg)) ;; -------------------------------------------------------------------------