-1

形式が不十分だったので、この質問を書き直しています。

(define (reduce f)                                                        
((lambda (value) (if (equal? value f) f (reduce value)))                 
(let r ((f f) (g ()))                                                  
(cond ((not (pair? f))                                                
(if (null? g) f (if (eq? f (car g)) (cadr g) (r f (caddr g))))) 
((and (pair? (car f)) (= 2 (length f)) (eq? 'lambda (caar f)))  
(r (caddar f) (list (cadar f) (r (cadr f) g) g)))              
((and (not (null? g)) (= 3 (length f)) (eq? 'lambda (car f)))    
(cons 'lambda (r (cdr f) (list (cadr f) (gensym (cadr f)) g))))  
(else (map (lambda (x) (r x g)) f))))))                          

; (reduce '((lambda x x) 3)) ==> 3
; (reduce '((lambda x (x x)) (lambda x (lambda y (x y)))))
;   ==> (lambda #[promise 2] (lambda #[promise 3] (#[promise 2] #[promise 3])))

; Comments: f is the form to be evaluated, and g is the local assignment
; function; g has the structure (variable value g2), where g2 contains
; the rest of the assignments.  The named let function r executes one
; pass through a form.  The arguments to r are a form f, and an
; assignment function g.  Line 2: continue to process the form until
; there are no more conversions left.  Line 4 (substitution): If f is
; atomic [or if it is a promise], check to see if matches any variable
; in g and if so replace it with the new value.  Line 6 (beta
; reduction): if f has the form ((lambda variable body) argument), it is
; a lambda form being applied to an argument, so perform lambda
; conversion.  Remember to evaluate the argument too!  Line 8 (alpha
; reduction): if f has the form (lambda variable body), replace the
; variable and its free occurences in the body with a unique object to
; prevent accidental variable collision.  [In this implementation a
; unique object is constructed by building a promise.  Note that the
; identity of the original variable can be recovered if you ever care by
; forcing the promise.]  Line 10: recurse down the subparts of f.

ラムダ式でラムダ削減を行う上記のコードがあります(これが私が欲しいものです)。ここでの私の問題は、誰かがこの実装を書き直すのを手伝ってくれることです(私はSchemeの経験がないので)。これにより、本体からアルファ変換を行う部分を抽出し、それを別の関数に入れます。ベータ還元も。関数 reduce は再帰的であるため、新しく作成された 2 つの関数は single-step である必要があります。つまり、1 つの境界付き変数のみを変換し、1 つの式のみを還元します。

4

1 に答える 1

1

あなたの一連の要件は、これが宿題であることを私にはかなり明確にしています。私が間違っている場合は、制約がどこから来たのか教えてください:)。

ソリューションの開発を開始する前に、質問をよりよく理解する必要があるように思えます。あなたが最初に言及したのはアルファ変換であり、そこから始めるのが良いと思います。アルファ変換関数を呼び出す方法と、それが返す可能性のあるものの例をいくつか書いていただけますか?

于 2012-03-21T16:47:15.157 に答える