方案函数中的右括号放置不当

发布于 2024-10-20 19:22:49 字数 1024 浏览 2 评论 0原文

我有以下方案函数:

(define get-ivars
  (λ (ivars num)
    (cond ((null? ivars) '())
          (else
           (append (list (car ivars) `(nth args ,num)) (list (get-ivars (cdr ivars) (+ num 1))))))))

在特定实例中返回以下内容:

(x (nth args 1) (y (nth args 2) ()))

问题是,我需要它返回:

((x (nth args1)) (y (nth args 2)) ())

- 末尾的两个右括号应该在 (nth 语句之后。

我如何将其变为 工作正常吗?

get-ivars 调用者

(define gen-classes
  (λ (classes)
    (cond ((null? classes) '())
          (else
           (let* ((class (car classes)))
             (eval
              `(define ,(cadr class)
                 (λ (args)
                   (let (
                          ,(get-ivars (cdr (cadddr class)) 1)
                          )
                     (eval
                      (let* ,(cdar (cddddr class))
                        (λ (method . args)
                          ,(get-methods (cdadr (cddddr class)))
     ))))))))))))

I have the following scheme function:

(define get-ivars
  (λ (ivars num)
    (cond ((null? ivars) '())
          (else
           (append (list (car ivars) `(nth args ,num)) (list (get-ivars (cdr ivars) (+ num 1))))))))

That returns the following in a specific instance:

(x (nth args 1) (y (nth args 2) ()))

The problem is, I need it to return:

((x (nth args1)) (y (nth args 2)) ())

-the two closing parenthesis at the end should be after the (nth statements.

How would I go about getting this to work properly?

get-ivars caller:

(define gen-classes
  (λ (classes)
    (cond ((null? classes) '())
          (else
           (let* ((class (car classes)))
             (eval
              `(define ,(cadr class)
                 (λ (args)
                   (let (
                          ,(get-ivars (cdr (cadddr class)) 1)
                          )
                     (eval
                      (let* ,(cdar (cddddr class))
                        (λ (method . args)
                          ,(get-methods (cdadr (cddddr class)))
     ))))))))))))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-10-27 19:22:49

else 子句中的第二个 (list ...) 才是让你陷入困境的原因。它使每个连续的调用嵌套得越来越深。递归自然会创建列表;你不需要再把它包裹起来。

尝试:

(define get-ivars
  (λ (ivars num)
    (if (null? ivars) '()
      (cons (list (car ivars) `(nth args ,num)) 
            (get-ivars (cdr ivars) (+ num 1))))))

关于 get-ivars 调用者代码,对 get-ivars 的未引用调用周围的括号是给您在评论中提到的麻烦的原因。有了它们,这段代码:

`(define ClassName
   (lambda (args)
     (let (,(get-ivars '(iVar1 iVar2 iVar3) 1))
       ;; your method-getting code
       )))

给你这个:

(define ClassName
  (lambda (args)
    (let (((iVar1 (nth args 1))
           (iVar2 (nth args 2))
           (iVar3 (nth args 3))))
      ;; method-getting code
     )))

正如你所看到的,它为你在 let 中的赋值周围提供了一组额外的括号。

所以你想这样做:

`(define ClassName
   (lambda (args)
     (let ,(get-ivars '(iVar1 iVar2 iVar3) 1)
        ;; your method-getting code
      )))

get-ivars返回一个列表列表,这正是你想要的let中的分配,所以你不需要包裹或(就像我之前那样)拼接它。只需单独使用取消引号,结果是:

(define ClassName
  (lambda (args)
    (let ((iVar1 (nth args 1))
          (iVar2 (nth args 2))
          (iVar3 (nth args 3)))
      ;; method-getting code
     )))

哪个应该可以解决问题。

顺便说一句,我发现当我在玩这个时,放弃eval很有帮助;然后可以目视检查结果以确保其语法正确。

That second (list ...) in your else clause is what's screwing you up. It's nesting each successive call deeper and deeper. The recursion will naturally create the list; you don't need to wrap it again.

Try:

(define get-ivars
  (λ (ivars num)
    (if (null? ivars) '()
      (cons (list (car ivars) `(nth args ,num)) 
            (get-ivars (cdr ivars) (+ num 1))))))

Regarding the get-ivars caller code, the parentheses surrounding the unquoted call to get-ivars are what's giving you the trouble you mention in the comments. With them, this code:

`(define ClassName
   (lambda (args)
     (let (,(get-ivars '(iVar1 iVar2 iVar3) 1))
       ;; your method-getting code
       )))

Gives you this:

(define ClassName
  (lambda (args)
    (let (((iVar1 (nth args 1))
           (iVar2 (nth args 2))
           (iVar3 (nth args 3))))
      ;; method-getting code
     )))

Which, as you can see, gives you an extra set of parentheses around the assignments in the let.

So you want to do this:

`(define ClassName
   (lambda (args)
     (let ,(get-ivars '(iVar1 iVar2 iVar3) 1)
        ;; your method-getting code
      )))

get-ivars is returning a list of lists, which is exactly what you want for the assignments in the let, so you don't need to wrap or (as I had it earlier) splice it. Just use the unquote on its own, and the result is:

(define ClassName
  (lambda (args)
    (let ((iVar1 (nth args 1))
          (iVar2 (nth args 2))
          (iVar3 (nth args 3)))
      ;; method-getting code
     )))

Which should do the trick.

Incidentally, I found it helpful to leave off the eval when I was playing around with this; one can then visually inspect the result to make sure its syntax is okay.

鹿! 2024-10-27 19:22:49

我没有尝试过,但我认为这会起作用:

(define (get-ivars ivars num)
    (if (null? ivars)
    '()
    (list (list (car ivars) `(nth args ,num))
          (get-ivars (cdr ivars) (1+ num)))))

I haven't tried this, but I think this would work:

(define (get-ivars ivars num)
    (if (null? ivars)
    '()
    (list (list (car ivars) `(nth args ,num))
          (get-ivars (cdr ivars) (1+ num)))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文