[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Self-evaluating function and closure

From: Mark H Weaver
Subject: Re: Self-evaluating function and closure
Date: Sun, 16 Jun 2019 07:29:59 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux)

Hello again,

Vladimir Zhbanov <address@hidden> writes:

> scheme@(guile-user)> (define (function-generator)
>                        (let ((func #f))                         
>                          (lambda () (set! func (let a () a)) func)))


> - Is there a way to work around this (either using the above 'let'
>   construct or anything else)?

Ideally, the code would be reworked to not expect equivalent procedures
to be distinguishable.  However, I should probably offer a hacky but
expedient workaround.  Here's one way to make otherwise equivalent
procedures distinguishable:

Allocate a fresh tag using (list #f), and arrange for the procedure to
return that tag if it's called with a special input that's outside of
the normal domain.  Note that for Scheme procedures, the "input" is in
general a list of arguments of arbitrary length.  You could use
'case-lambda', which creates procedures that evaluate different body
expressions depending on how many arguments are passed to it.  Just add
a case for an arity that you will never use, which returns the unique

In the example you gave, (let a () a) is equivalent to:

  ((letrec ((a (lambda () a)))

The procedure returned by (let a () a) expects 0 arguments.  It will
raise an error otherwise.  We can repurpose the previously erroneous
arity-1 case to return the unique tag, as follows:

  (let ((unique-tag (list #f)))
    ((letrec ((a (case-lambda
                   (() a)
                   ((x) unique-tag))))

Every time the above expression is evaluated, it will necessarily return
a unique procedure, which, if passed 0 arguments, behaves the same as
the procedure returned by (let a () a).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]