Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The implement of mutex in SICP Section 3.4

When the authors implement the mutex part of serializers, they use a list called cell. But the list only contains one element, so why not just use a variable?

like image 588
Bin Wang Avatar asked Dec 19 '11 02:12

Bin Wang


2 Answers

Because a variable isn't a first-class value that you can pass to another function. In 3.4, the authors implement a make-mutex function that uses clear! as a helper function, which takes a cell. If the cell were represented by a mutable variable, then clear! would have to be defined inside make-mutex! to close over that variable. The same goes for the test-and-set! helper function.

They also could have used, say, a box instead of a cons cell.

like image 92
Asumu Takikawa Avatar answered Oct 28 '22 17:10

Asumu Takikawa


If a variable is used there instead of a list, the procedures clear! and test-and-set! won't work since Scheme is pass-by-value.

like image 5
pjhades Avatar answered Oct 28 '22 19:10

pjhades