为什么我在DrRacket中出现函数体错误?

时间:2014-11-12 14:36:38

标签: scheme racket

我一直在DrRacket中得到错误:define:期望只有一个函数体的表达式,但在其中找到了1个额外部分:

(cond ((empty? (rest a-list) (first a-list))) (huffman-tree (insert-node (newn (first a-list) (first (rest a-list)) (rest (rest a-list))))))

这是我的代码:

(define (huffman-tree a-list)
  (local(
         (define (newn altn1 altn2)
           (cond [(empty? altn2) altn1]
                 [else (make-node (node-frequency altn1) (node-frequency altn2))
                       (string-append (node-value alt n1) (node-value altn2)) altn1 altn2]))))
  (cond [(empty? (rest a-list) (first a-list))]
        (huffman-tree (insert-node (newn (first a-list)
                                         (first (rest a-list)) (rest (rest a-list))))))
        )

目前我们还不知道我们做错了什么。

1 个答案:

答案 0 :(得分:1)

代码中存在一些语法错误,我们无法对其进行测试,因为它引用了您未发布的几个辅助函数,并且我们不知道示例输入和预期的产出。

只需修改代码的格式就可以解决一些错误(某些括号不平衡!),我可以提供帮助,但我无法保证您的算法对于前面提到的原因是正确的。看看是否有效:

(define (huffman-tree a-list)
  (local [(define (newn altn1 altn2)
            (cond [(empty? altn2) altn1]
                  [else (make-node (node-frequency altn1)
                                   (node-frequency altn2))
                        (string-append (node-value altn1)
                                       (node-value altn2)) altn1 altn2]))]
    (cond [(empty? (rest a-list) (first a-list))]
          (huffman-tree (insert-node 
                         (newn (first a-list)
                               (first (rest a-list)) (rest (rest a-list))))))))