commit d1f476d505795e81c81b09753a0eaba294a03e06 parent 1af37461d13be37dcb9232cd3dc448f65a8a3d99 Author: Yuval Langer <yuval.langer@gmail.com> Date: Wed, 5 Apr 2023 15:37:40 +0300 Add solution to exercise 3.6. Diffstat:
M | Makefile | | | 3 | +++ |
A | sicp/solutions/3_6.scm | | | 17 | +++++++++++++++++ |
A | sicp/tests/3_6.scm | | | 22 | ++++++++++++++++++++++ |
3 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile @@ -1,6 +1,9 @@ poop: echo poop +3.6: + guile -L . sicp/tests/3_6.scm + 2.75: guile -L . sicp/tests/2_75.scm diff --git a/sicp/solutions/3_6.scm b/sicp/solutions/3_6.scm @@ -0,0 +1,17 @@ +(define-library (sicp solutions 3_6) + (import (scheme base)) + (import (only (guile) + *random-state* + random:uniform + seed->random-state + )) + (export rand) + + (begin + (define (rand command . value) + (cond + ((eq? 'generate command) + (random:uniform)) + ((eq? 'reset command) + (set! *random-state* + (seed->random-state (car value)))))))) diff --git a/sicp/tests/3_6.scm b/sicp/tests/3_6.scm @@ -0,0 +1,22 @@ +(import (srfi :64)) +(import (sicp solutions 3_6)) + +(rand 'reset 0) + +(define random-sequence-a + (map + (lambda (x) (rand 'generate)) + (iota 10))) + +(rand 'reset 0) + +(define random-sequence-b + (map + (lambda (x) (rand 'generate)) + (iota 10))) + +(test-begin "3.6") +(test-equal + random-sequence-a + random-sequence-b) +(test-end "3.6")