Question: In Example 8.52 we noted that functional languages can safely use reference counts since the lack of an assignment statement prevents them from introducing circularity.

In Example 8.52 we noted that functional languages can safely use reference counts since the lack of an assignment statement prevents them from introducing circularity. This isn’t strictly true; constructs like the Lisp letrec can also be used to make cycles, so long as uses of circularly defined names are hidden inside lambda expressions in each definition:

(define foo (lambda () (letrec ((a (lambda (f) (if f #\A b))) (b (lambda (f) (if f #\B c))) (c (lambda (f) (if f #\C a)))) a)))

Each of the functions a, b, and c contains a reference to the next:

(define foo (lambda () (letrec ((a (lambda (f) (if f #\A b)))

How might you address this circularity without giving up on reference counts?

(define foo (lambda () (letrec ((a (lambda (f) (if f #\A b))) (b (lambda (f) (if f #\B c))) (c (lambda (f) (if f #\C a)))) a)))

Step by Step Solution

3.34 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

In a pure functional language data is immut... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!