忽略/删除Scheme中频率列表中的重复符号

时间:2015-11-03 21:09:15

标签: list scheme

如果符号在列表中显示的次数超过1次,我不确定如何使此功能仅返回符号的频率一次。目前(freq-list '(This is a really really good test))返回((this . 1) (is . 1) (a . 1) (really . 2) (really . 1) (good . 1) (test . 1))但我希望只显示(really . 2),并提前省略(really . 1)谢谢。

(define (num-occurs sym lst)
  (define (num-occurs-helper sym lst counter)
    (cond ((null? lst) counter)
          ((equal? sym (car lst))(num-occurs-helper sym (cdr lst) (+ counter 1)))
          (else (num-occurs-helper sym (cdr lst) counter))))
  (num-occurs-helper sym lst 0))

(define (freq-list lst)
  (cond ((null? lst) '())
        (else (cons
               (cons (car lst)
                     (num-occurs (car lst) lst))
               (freq-list (cdr lst))))))

0 个答案:

没有答案