错误我看不到

Error I can't see

(define (length2 item)
  (define (test item count)
  (if (null? item)
      count
     ((+ 1 count) (test item (cdr item)))))
    (test item 0))

得到这个错误:

+:违约 预期:数字? 给定:(2 3) 论点位置:第二 其他参数。:

我不明白怎么了?我试着让它迭代。

你在递归中传递参数的方式有问题,注意 count 是第二个参数。这应该可以解决它:

(define (length2 item)
  (define (test item count)
    (if (null? item)
        count
        (test (cdr item) (+ 1 count))))
  (test item 0))

它按预期工作:

(length2 '(1 2 3 4 5))
=> 5