Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. ;;; defmac (define macro) and defsmac (define simple macro) are like defun,
  2. ;;; but they define the function as a macro instead of as an expr. They are
  3. ;;; less powerful then defining a macro directly (for example, they cannot be
  4. ;;; used to define macros with arbitrarily many arguments) but are easier to
  5. ;;; use. For example,
  6. ;;;
  7. ;;; (defsmac f (x y) (foo (bar x) (bar y)))
  8. ;;;
  9. ;;; causes f to be defined as a macro in such a way that every call (f e1 e2)
  10. ;;; will expand to (foo (bar e1) (bar e2)) before it is evaluated.
  11.  
  12. (defun defsmac macro (args)
  13. (defsmac1 (cadr args) (caddr args) (cdddr args)))
  14.  
  15.  
  16. (defun defsmac1 (name formals body)
  17. `(defun ,name macro (app)
  18. ,(defsmac2 formals
  19. (cond ((cdr body) (cons 'progn body))
  20. (t (car body))))))
  21.  
  22. (defun defsmac2 (formals body)
  23. `(sublis ,(defsmac3 formals 1) (quote ,body)))
  24.  
  25. (defun defsmac3 (formals n)
  26. (cond ((null formals) nil)
  27. (`(cons (cons (quote ,(car formals)) (car ,(defsmac4 n)))
  28. ,(defsmac3 (cdr formals) (1+ n))))))
  29.  
  30. (defun defsmac4 (n) (cond ((= n 0) 'app) ((list 'cdr (defsmac4 (1- n))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement