Scheme中的频率列表

时间:2015-06-24 15:49:51

标签: scheme

我想编写一个函数来查找每个元素在列表中出现的次数。例如,对于列表'(1 1 1 2 2 3 4),1发生三次,2发生两次,3和4都发生一次。所以输出应该是这样的 ((1 3) (2 2) (3 1) (4 1))。这是我写的代码:

(define (aa-helper list counter)
  (cond [(null? list) '()]
        [(eq? (car list) (cadr list)) (aa-helper (cdr list) (+ counter 1))]
        [else (cons (car list) counter) (aa-helper (cdr list) 1)]))

(define (aa list)
  (aa-helper list 1))

但我收到了这个错误:

cadr: contract violation
  expected: (cons/c any/c pair?)
  given: '(4)

0 个答案:

没有答案