usage.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (import
  2. (except (rnrs base) let-values map)
  3. (only (guile)
  4. lambda* λ
  5. simple-format
  6. display)
  7. (stream))
  8. ;; Define abstraction for fractions.
  9. (define numerator
  10. (λ (fraction)
  11. (car fraction)))
  12. (define denominator
  13. (λ (fraction)
  14. (cdr fraction)))
  15. (define make-fraction
  16. (λ (num denom)
  17. (cons num denom)))
  18. ;; natural numbers
  19. (define natural-numbers
  20. (make-stream 0 0 0
  21. (λ (val index prev-input) (+ prev-input 1))
  22. (λ (x) x)))
  23. (display
  24. (simple-format
  25. #f "third natural number: ~a\n" (stream-get-nth-value natural-numbers 3)))
  26. ;; If we have a function calculating a next value from a
  27. ;; previous value, it is easy to use that function for
  28. ;; creating a stream, as we will see in the example of
  29. ;; fractions.
  30. (define next-rational-fraction
  31. (λ (fraction)
  32. (let ([num (numerator fraction)]
  33. [denom (denominator fraction)])
  34. (cond [(= num 1)
  35. (if (odd? denom)
  36. (make-fraction num
  37. (+ denom 1))
  38. (make-fraction (+ num 1)
  39. (- denom 1)))]
  40. [(= denom 1)
  41. (if (odd? num)
  42. (make-fraction (- num 1)
  43. (+ denom 1))
  44. (make-fraction (+ num 1)
  45. denom))]
  46. [else (if (odd? (+ num denom))
  47. (make-fraction (+ num 1)
  48. (- denom 1))
  49. (make-fraction (- num 1)
  50. (+ denom 1)))]))))
  51. ;; Build a stream using next-rational-fraction.
  52. (define rational-numbers
  53. (make-stream (make-fraction 1 1)
  54. 0
  55. 0
  56. (λ (val index prev-input) val)
  57. next-rational-fraction))
  58. (display
  59. (simple-format
  60. #f "fourth fraction: ~a\n" (stream-get-nth-value rational-numbers 5)))
  61. (display
  62. (simple-format
  63. #f "position of 7/8 in the fractions stream: ~a\n"
  64. (stream-pos (make-fraction 7 8) rational-numbers)))
  65. (display
  66. (simple-format
  67. #f "97. fraction: ~a\n"
  68. (stream-get-nth-value rational-numbers 97)))
  69. (display
  70. (simple-format
  71. #f "double of third natural number: ~a\n"
  72. (let ([double-natural-numbers-stream
  73. (stream-map (λ (x) (* x 2))
  74. natural-numbers)])
  75. (stream-get-nth-value double-natural-numbers-stream 3))))
  76. (display
  77. (simple-format
  78. #f "double of hundredth natural number: ~a\n"
  79. (let ([double-natural-numbers-stream
  80. (stream-map (λ (x) (* x 2))
  81. natural-numbers)])
  82. (stream-get-nth-value double-natural-numbers-stream 100))))
  83. (display
  84. (simple-format
  85. #f "111. square natural number: ~a\n"
  86. (let ([square-natural-numbers-stream
  87. (stream-map (λ (x) (* x x))
  88. natural-numbers)])
  89. (stream-get-nth-value square-natural-numbers-stream 111))))
  90. (display
  91. (simple-format
  92. #f "first 20 square numbers: ~a\n"
  93. (let ([square-natural-numbers-stream
  94. (stream-map (λ (x) (* x x))
  95. natural-numbers)])
  96. (stream->list square-natural-numbers-stream 20))))