【问题标题】:Scheme lambda reduction improvement方案 lambda 减少改进
【发布时间】:2012-03-21 08:45:03
【问题描述】:

我正在重写这个问题,因为它的格式很差。

(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.

我有上面的代码,它对 lambda 表达式(这是我想要的)进行 lambda 缩减。我的问题是,有人可以帮我重写这个实现(因为我对Scheme没有那么丰富的经验),以便我从正文中提取进行alpha转换的部分并将其放入单独的函数中,以及执行的部分β-减少也是如此。函数reduce是递归的,所以两个新创建的函数需要是单步的,这意味着它们将只转换一个有界变量并且只减少一个表达式。

【问题讨论】:

  • 仅供未来访问者参考,这似乎是改进this question 的尝试。 @user,将来,在这种情况下,您应该编辑原始帖子,而不是发布新问题。编辑使一切更有条理,帮助您重新打开已关闭的问题,并避免您被禁止提问。
  • 上述问题中的cadr是什么?

标签: scheme lambda-calculus


【解决方案1】:

你的要求让我很清楚这是家庭作业;如果我弄错了,请告诉我你的约束来自哪里:)。

在我看来,您需要在开始开发解决方案之前更好地理解问题。您提到的第一件事是 alpha 转换,我认为这是一个很好的起点。您能否写一些示例来说明如何调用 alpha 转换函数以及它可能返回的内容?

【讨论】:

  • 好吧,例如在这种情况下 ((λ x (λ y (x y))) y) ,我们需要在 "(λ y (x y))" 中进行 alpha 转换,将 y 替换为 y1 所以它将导致 ((λ x (λ y1 (x y1))) y),然后对其进行 beta 缩减,将 x 替换为 y,我们得到 (λ y1 (y y1))。您只能编辑评论每 5 秒一次。
猜你喜欢
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多