Add exercise 3.8

This commit is contained in:
Oliver Payne 2022-04-22 14:44:15 +01:00
parent 3d8bad6a34
commit 91d3bafd2c
1 changed files with 24 additions and 0 deletions

24
3_8.rkt Normal file
View File

@ -0,0 +1,24 @@
#lang sicp
(define f
(let ((x 0))
(lambda (n)
(let ((old-x x))
(begin
(set! x n)
(cond ((= n 0)
(if (= old-x 0) -1 0))
(else n)))))))
(define left-to-right
(let ((left (f 0))
(right (f 1)))
(+ left right)))
(define right-to-left
(let ((right (f 1))
(left (f 0)))
(+ left right)))
;; (f 0) first: (+ (f 0) (f 1)) -> 0
;; (f 1) first: (+ (f 0) (f 1)) -> 1