开发者

Basic function with list

We have a list A (1 2 3 4 5 2 2 3 3 3 4 6 7) and we want to get this numbers which number of meets are < then them.

numbers | meets
      1 = 1
      2 < 3
      3 < 4
      5 > 1
      4 > 2
      6 > 1
      7 > 1

Example:
Input:
list (1开发者_运维问答 1 2 3 2 3 4 4 5 5 5 5 5 5)
Output:
(1 3 4)


My solution, I followed @lbruder comment, but it's not clear to me why 1 appears in your output since if you were checking for smaller and equal 2 should have appeared also

> (define (occurences lst)
    (cond ((null? lst) empty)
          ((< (length (filter (lambda (x) (if (= x (car lst)) x #f)) lst)) (car lst)) (cons (car lst) (occurences (filter (lambda (x) (if (not (= x (car lst))) x #f)) lst))))
          (else (occurences (filter (lambda (x) (if (not (= x (car lst))) x #f)) lst)))))
> (occurences '(1 2 3 2 3 4 4 5 5 5 5 5 5))
(3 4)
  • number 1 occurrence 1 -> false 1 !< 1
  • number 2 occurrence 2 -> false 2 !< 2
  • number 3 occurrence 2 -> true 2 < 3
  • number 4 occurrence 2 -> true 2 < 4
  • number 5 occurrence 6 -> false 6 !< 5

Hence (3 4)

By !< , I mean not smaller (greater or equal)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜