计算Scheme中的唯一元素

发布于 2024-10-20 12:37:12 字数 381 浏览 8 评论 0原文

编写一个递归Scheme过程count-dist elements,它接受一个包含重复元素的列表,并返回列表中不同元素的数量。

这是我的代码,但它无法正常工作。请帮忙!谢谢!!

(define (count-dist-elements lis)   
  (cond
    ((null? lis) 0)
    ((null? (cdr lis))0)
    ((member (car lis)(cdr lis)))
     (else(+ 1(count-dist-elements (cdr lis))))))

p/s: 就这样吧 (count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9))

Write a recursive Scheme procedure count-dist elements that takes a list with duplicate elements and returns the number of distinct elements in the list.

This is my code, but it is not working correctly. Please help! Thanks!!

(define (count-dist-elements lis)   
  (cond
    ((null? lis) 0)
    ((null? (cdr lis))0)
    ((member (car lis)(cdr lis)))
     (else(+ 1(count-dist-elements (cdr lis))))))

p/s: let it be (count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-10-27 12:37:12

看起来你已经很接近了。

  • 当您向函数传递一个包含一个元素的列表时会发生什么?在这种情况下你的函数应该返回什么?
  • 具有相同元素的二元素列表(例如 (5 5))怎么样?你的函数返回一个合理的值吗?

It looks like you're getting pretty close.

  • What happens when you pass your function a list with one element? What should your function return in this case?
  • What about a two-element list with the same element (eg. (5 5))? Does your function return a sensible value?
北方的韩爷 2024-10-27 12:37:12

第一:为什么在 (null? (cdr lis)) 情况下返回零?

第二:如果第一个元素也出现在列表的后面,您认为您的代码会返回什么?你确定吗?

First: Why are you returning zero in the (null? (cdr lis)) case?

Second: What do you think your code returns in the case where the first element also occurs later in the list? Are you sure?

风情万种。 2024-10-27 12:37:12
 (define (count-dist-elements lst dist-elems count)
   (cond ((null? lst) count)
         ((member (car lst) dist-elems)
          (count-dist-elements (cdr lst) dist-elems count))
         (else
          (count-dist-elements (cdr lst)
                               (cons (car lst) dist-elems)
                               (+ 1 count)))))

(count-dist-elements '(abbc) '() 0) ==>; 3

(count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9) '() 0) ==> 9

或者,如果您希望它递归而不是迭代(并且它必须使用如图所示的函数调用),

(define (count-dist-elements lst . dist-elems)
  (let ((dist-elems (if (null? dist-elems) '() (car dist-elems))))
    (cond ((null? lst) 0)
          ((member (car lst) dist-elems)
           (count-dist-elements (cdr lst) dist-elems))
          (else
           (+ 1 (count-dist-elements (cdr lst) (cons (car lst) dist-elems)))))))

则会给出相同的结果。

(count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9)) ==>; <代码>9

 (define (count-dist-elements lst dist-elems count)
   (cond ((null? lst) count)
         ((member (car lst) dist-elems)
          (count-dist-elements (cdr lst) dist-elems count))
         (else
          (count-dist-elements (cdr lst)
                               (cons (car lst) dist-elems)
                               (+ 1 count)))))

(count-dist-elements '(a b b c) '() 0) ==> 3

(count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9) '() 0) ==> 9

Or, if you want it recursive and not iterative (and, it has to use a function call like the one shown),

(define (count-dist-elements lst . dist-elems)
  (let ((dist-elems (if (null? dist-elems) '() (car dist-elems))))
    (cond ((null? lst) 0)
          ((member (car lst) dist-elems)
           (count-dist-elements (cdr lst) dist-elems))
          (else
           (+ 1 (count-dist-elements (cdr lst) (cons (car lst) dist-elems)))))))

Gives the same results.

(count-dist-elements '(1 2 1 1 2 3 4 5 5 6 6 7 7 8 8 8 9)) ==> 9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文