;; DTrans.scm ;; NKU CSC 601 - Fall 2002 - Kirby ;; ---------------------------------------------------------------------- ;; An example of a hack'n'eval Scheme program for making syntactic sugar. ;; (Usually macros do this.) ;; ;; Allows an [allegedly] friendlier syntax for function definitions. ;; For example, one can write: ;; ;; (def cube (x) ; (* x x x)) ;; ;; instead of ;; ;; (define cube ;; (lambda (s1 s2) ;; (* x x x))) ;; ---------------------------------------------------------------------- (define dtrans (lambda (d) (if (eq? 'def (car d)) (let ((funcname (cadr d)) (args-and-body (cddr d))) (eval (list 'define funcname (cons 'lambda args-and-body)))) (eval d)))) ;; Example usage ;; ------------- (map dtrans '( ;; Define functions the "new way". (def longer? (s1 s2) (< (length s1) (length s2))) (def cube (x) (* x x x)) (def tri-area (base height) (* base height)) ;; dtrans allows traditional defs too. (define square (lambda (x) (* x x))) )) (longer? '(a b c) '(a b c d e)) (longer? '(a b) '(c)) (cube 3) (tri-area 10 10) (square 9)