Skip to content
scrambled
Programming Language Theory Flashcard

How to implement self-reference with and without mutation?

Books Deckgeneral

Question

How to implement self-reference with and without mutation?

Answer

With mutation:

racket
(define (mk-self-ref)
	(let ([self 'dummy])
		(set!
			self
			(lambda (m)
				(case (m)
					[(get-self) (lambda () self)])))))

Without mutation:

racket
(define (self-app x)
	(x x))

(define (mk-self-ref)
	(self-app
		(lambda (self)
			(lambda (m)
				(case (m)
					[(get-self) (lambda () self)])))))