home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacScheme20 / Contributed / ExtendSyntax / extendsyntax.examples next >
Encoding:
Text File  |  1987-12-14  |  1.8 KB  |  68 lines  |  [TEXT/EDIT]

  1. ; The general syntax is
  2. ;
  3. ; (extend-syntax (name key ...)
  4. ;   (pattern [fender] expansion)
  5. ;   ...)
  6. ;
  7. ; where each fender can be omitted and usually is.
  8. ; The name is the name of the macro being defined.
  9. ; The keys are symbols that are specially recognized by the macro,
  10. ; like the else in a cond expression.
  11. ; Each pattern is a pattern for the input.
  12. ; Each expansion shows what the expanded form of the macro should
  13. ; look like if its pattern matches.
  14. ; If a fender is specified, it is an arbitrary Scheme expression
  15. ; that is evaluated when the pattern matches.  If it returns false,
  16. ; then the pattern is treated as though it didn't really match.
  17.  
  18. ; Here's how the let macro could have been defined:
  19.  
  20. (extend-syntax (lett)
  21.                ((lett ((x e) ...) body ...)
  22.                 ((lambda (x ...) body ...)
  23.                  e ...)))
  24.  
  25. (lett ((x 3) (y 4))
  26.   (+ x y))
  27.  
  28. ; Here's a definition of progn as in Common Lisp (it's like begin):
  29.  
  30. (extend-syntax (progn)
  31.                ((progn) #f)
  32.                ((progn e) e)
  33.                ((progn e1 e2 ...)
  34.                 (begin e1 (progn e2 ...))))
  35.  
  36. (progn)
  37. (progn 3)
  38. (progn (write 1) (write 2) (write 3) 'go)
  39.  
  40. ; Here's an overly complicated definition of cond:
  41.  
  42. (extend-syntax (cond else)
  43.                ((cond) #f)
  44.                ((cond (else e1 e2 ...))
  45.                 (begin e1 e2 ...))
  46.                ((cond (e)) e)
  47.                ((cond (e) clause ...)
  48.                 (or e (cond clause ...)))
  49.                ((cond (e e1 ...) clause ...)
  50.                 (if e
  51.                     (begin e1 ...)
  52.                     (cond clause ...))))
  53.  
  54. (cond)
  55.  
  56. (cond (else 14))
  57.  
  58. (cond (34))
  59.  
  60. (cond (34) (else 19))
  61.  
  62. (cond ((zero? 10) 20)
  63.       ((positive? 10) 30)
  64.       (else 40))
  65.  
  66. ; extend-syntax has several features that aren't illustrated here.
  67. ; Get Kent Dybvig's book if you're interested.
  68.