srfi-5.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Rewritten, simplified, and corrected from the SRFI document.
  3. ;
  4. ; The SRFI implementation gets the scoping wrong for the name. It is visible
  5. ; to the arguments and should not be.
  6. (define-syntax let
  7. (syntax-rules ()
  8. ; If no name we go straight to the standard LET.
  9. ((let () body ...)
  10. (standard-let () body ...))
  11. ((let ((variable value) ...) body ...)
  12. (standard-let ((variable value) ...) body ...))
  13. ; Rest binding
  14. ((let ((var val) . bindings) body ...)
  15. (let-loop #f bindings (var) (val) (body ...)))
  16. ;; Signature-style and standard named LET.
  17. ((let (name bindings ...) body ...)
  18. (let-loop name (bindings ...) () () (body ...)))
  19. ((let name bindings body ...)
  20. (let-loop name bindings () () (body ...)))))
  21. ; A loop to walk down the list of bindings.
  22. (define-syntax let-loop
  23. (syntax-rules ()
  24. ; No more bindings - make a LETREC.
  25. ((let-loop name () (vars ...) (vals ...) body)
  26. ((letrec ((name (lambda (vars ...) . body)))
  27. name)
  28. vals ...))
  29. ; Rest binding, no name
  30. ((let-loop #f (rest-var rest-val ...) (var ...) (val ...) body)
  31. (standard-let ((var val) ... (rest-var (list rest-val ...))) . body))
  32. ; Process a (var val) pair.
  33. ((let-loop name ((var val) more ...) (vars ...) (vals ...) body)
  34. (let-loop name (more ...) (vars ... var) (vals ... val) body))
  35. ; End with a rest variable - make a LETREC.
  36. ((let-loop name (rest-var rest-vals ...) (vars ...) (vals ...) body)
  37. ((letrec ((name (lambda (vars ... . rest-var) . body)))
  38. name)
  39. vals ... rest-vals ...))))
  40. ; Four loops - normal and `signature-style', each with and without a rest
  41. ; binding.
  42. ;
  43. ;(let fibonacci ((n 10) (i 0) (f0 0) (f1 1))
  44. ; (if (= i n)
  45. ; f0
  46. ; (fibonacci n (+ i 1) f1 (+ f0 f1))))
  47. ;
  48. ;(let (fibonacci (n 10) (i 0) (f0 0) (f1 1))
  49. ; (if (= i n)
  50. ; f0
  51. ; (fibonacci n (+ i 1) f1 (+ f0 f1))))
  52. ;
  53. ;(let fibonacci ((n 10) (i 0) . (f 0 1))
  54. ; (if (= i n)
  55. ; (car f)
  56. ; (fibonacci n (+ i 1) (cadr f) (+ (car f) (cadr f)))))
  57. ;
  58. ;(let (fibonacci (n 10) (i 0) . (f 0 1))
  59. ; (if (= i n)
  60. ; (car f)
  61. ; (fibonacci n (+ i 1) (cadr f) (+ (car f) (cadr f)))))