srfi-2.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees
  3. ; The reference implementation is written in some weird Scheme variant.
  4. ; This is an attempt to produce the same result using SYNTAX-RULES.
  5. ; I found the both the specification and the implementation unhelpful.
  6. ; For example, one would think that (AND-LET* ()) -> #T by analogy with
  7. ; (AND) -> #T. The specification doesn't say.
  8. ;
  9. ; The following behaves correctly on the test cases at the end of the
  10. ; reference implementation, except that it doesn't catch the three syntax
  11. ; errors. There is no way for SYNTAX-RULES to distinguish between a
  12. ; constant and a variable, and no easy way to check if a variable is
  13. ; being used twice in the same AND-LET* (and why is that an error? LET*
  14. ; allows it).
  15. (define-syntax and-let*
  16. (syntax-rules ()
  17. ; No body - behave like AND.
  18. ((and-let* ())
  19. #t)
  20. ((and-let* ((var exp)))
  21. exp)
  22. ((and-let* ((exp)))
  23. exp)
  24. ((and-let* (var))
  25. var)
  26. ; Have body - behave like LET* but check for #F values.
  27. ; No clauses so just use the body.
  28. ((and-let* () . body)
  29. (begin . body))
  30. ; (VAR VAL) clause - bind the variable and check for #F.
  31. ((and-let* ((var val) more ...) . body)
  32. (let ((var val))
  33. (if var
  34. (and-let* (more ...) . body)
  35. #f)))
  36. ; Error check to catch illegal (A B ...) clauses.
  37. ((and-let* ((exp junk . more-junk) more ...) . body)
  38. (syntax-violation 'and-let*
  39. "syntax error"
  40. '(and-let* ((exp junk . more-junk) more ...) . body)))
  41. ; (EXP) and VAR - just check the value for #F.
  42. ; There is no way for us to check that VAR is an identifier and not a
  43. ; constant
  44. ((and-let* ((exp) more ...) . body)
  45. (if exp
  46. (and-let* (more ...) . body)
  47. #f))
  48. ((and-let* (var more ...) . body)
  49. (if var
  50. (and-let* (more ...) . body)
  51. #f))))
  52. ;(define-syntax expect
  53. ; (syntax-rules ()
  54. ; ((expect a b)
  55. ; (if (not (equal? a b))
  56. ; (assertion-violation 'expect "test failed" 'a b)))))
  57. ;
  58. ;(expect (and-let* () 1) 1)
  59. ;(expect (and-let* () 1 2) 2)
  60. ;(expect (and-let* () ) #t)
  61. ;
  62. ;(expect (let ((x #f)) (and-let* (x))) #f)
  63. ;(expect (let ((x 1)) (and-let* (x))) 1)
  64. ;(expect (and-let* ((x #f)) ) #f)
  65. ;(expect (and-let* ((x 1)) ) 1)
  66. ;;(must-be-a-syntax-error (and-let* ( #f (x 1))) )
  67. ;(expect (and-let* ( (#f) (x 1)) ) #f)
  68. ;;(must-be-a-syntax-error (and-let* (2 (x 1))) )
  69. ;(expect (and-let* ( (2) (x 1)) ) 1)
  70. ;(expect (and-let* ( (x 1) (2)) ) 2)
  71. ;(expect (let ((x #f)) (and-let* (x) x)) #f)
  72. ;(expect (let ((x "")) (and-let* (x) x)) "")
  73. ;(expect (let ((x "")) (and-let* (x) )) "")
  74. ;(expect (let ((x 1)) (and-let* (x) (+ x 1))) 2)
  75. ;(expect (let ((x #f)) (and-let* (x) (+ x 1))) #f)
  76. ;(expect (let ((x 1)) (and-let* (((positive? x))) (+ x 1))) 2)
  77. ;(expect (let ((x 1)) (and-let* (((positive? x))) )) #t)
  78. ;(expect (let ((x 0)) (and-let* (((positive? x))) (+ x 1))) #f)
  79. ;(expect (let ((x 1)) (and-let* (((positive? x)) (x (+ x 1))) (+ x 1))) 3)
  80. ;;(must-be-a-syntax-error
  81. ;; (let ((x 1)) (and-let* (((positive? x)) (x (+ x 1)) (x (+ x 1))) (+ x 1)))
  82. ;;)
  83. ;
  84. ;(expect (let ((x 1)) (and-let* (x ((positive? x))) (+ x 1))) 2)
  85. ;(expect (let ((x 1)) (and-let* ( ((begin x)) ((positive? x))) (+ x 1))) 2)
  86. ;(expect (let ((x 0)) (and-let* (x ((positive? x))) (+ x 1))) #f)
  87. ;(expect (let ((x #f)) (and-let* (x ((positive? x))) (+ x 1))) #f)
  88. ;(expect (let ((x #f)) (and-let* ( ((begin x)) ((positive? x))) (+ x 1))) #f)
  89. ;
  90. ;(expect (let ((x 1)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
  91. ;(expect (let ((x 0)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
  92. ;(expect (let ((x #f)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
  93. ;(expect (let ((x 3)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))) 3/2)