#lang racket ;; example of currying: turning n-ary functions into unary functions #;((λ (a b c) (- (string-length a) (/ b c))) "foo" 8 2) #;((((λ (a) (λ (b) (λ (c) (- (string-length a) (/ b c))))) "foo") 8) 2) ;; A LExpr (Lambda expression) is one of: ;; - Symbol <- identifier ;; - (list 'λ (list Symbol) LExpr) <- function ;; - (list LExpr LExpr) <- application ;; An ILExpr (Index Lambda expression) is one of: ;; - Nat ;; - (list 'λ ILExpr) ;; - (list ILExpr ILExpr) (define sample-lexpr '((λ (x) (λ (y) (λ (x) (y x)))) (λ (x) x))) (define sample-ilexpr '((λ (λ (λ (1 0)))) (λ 0))) ;; '() #;(;; '() (λ (x) ;; '(x) (λ (y) ;; '(y x) (λ (x);; '(x y x) (y x)))) ;; '() (λ (x) ;; '(x) x)) #;((λ (λ (λ (1 0)))) (λ 0)) ;; indexify : LExpr -> ILExpr ;; Make an index expression out of the lexpr (define (indexify lexpr) (define (indexify/args arguments lexpr) (match lexpr [(? symbol? identifier) (or (index-of arguments identifier) (error "unbound identifier"))] [(list 'λ (list (? symbol? argument)) body) (list 'λ (indexify/args (cons argument arguments) body))] [(list func arg) (list (indexify/args arguments func) (indexify/args arguments arg))])) (indexify/args '() lexpr)) (equal? (indexify sample-lexpr) sample-ilexpr) (indexify 'x)