Recursion 替换球拍中结构的元素

Recursion 替换球拍中结构的元素,recursion,boolean,scheme,racket,Recursion,Boolean,Scheme,Racket,假设我们有下面的表达式band'x bor'y'z,其中band和bor布尔结构包含arg1和arg2 如果我想通过表达式上的深度递归将变量“x”和“y”更改为“a”和“b”,我该怎么做?有一种特殊的形式,用于功能性地更新结构中的一些字段,在有很多字段的情况下使用它非常好: (struct person (name age occupation) #:transparent) (define p (person "Per" 19 "Painter")) (define (change-occu

假设我们有下面的表达式band'x bor'y'z,其中band和bor布尔结构包含arg1和arg2


如果我想通过表达式上的深度递归将变量“x”和“y”更改为“a”和“b”,我该怎么做?

有一种特殊的形式,用于功能性地更新结构中的一些字段,在有很多字段的情况下使用它非常好:

(struct person (name age occupation) #:transparent)
(define p (person "Per" 19 "Painter"))

(define (change-occupation p new-occupation)
  (struct-copy person p [occupation new-occupation]))

(change-occupation p "Programmer") ; ==> (person "Per" 19 "Programmer")
当然,这只是一种奇特的写作方式:

(define (change-occupation p new-occupation)
  (person (person-name p)
          (person-age p)
          new-occupation))
现在我不知道两个结构的名称,但您可能需要创建一个泛型访问器,除非其中一个是另一个的子类型:

(define (change-first obj new-value)
  (if (band? obj)
      (band new-value (band-second obj))
      (bor new-value (bor-arg2 obj))))

或者您也可以在您的过程中进行类似的案例分析。

请发布您迄今为止编写的代码,包括示例输入和预期输出,并指出给您带来麻烦的特定部分。别忘了让示例可执行,这样我们就可以测试并修复它。您可能会对此感兴趣。有很多好方法可以做到这一点,包括图书馆。