让我知道代码中的错误并编辑它

发布于 2024-10-18 20:27:55 字数 247 浏览 0 评论 0原文

定义一个名为 symcount 的函数,它接受一个符号和一个列表,并返回该符号在列表中出现的次数。如果列表包含子列表,则无论嵌套的深度如何,都应计算所有出现的次数。

(define syscount(lambda (n x)
  (if (empty? x)
    0
  (if (equal? n (car x))
    (+ 1 syscount(n (cdr x))))))) 

这是我写的,请帮助我

Define a function called symcount that takes a symbol and a list and returns the number of times the symbol occurs in the list. If the list contains sublists, all occurrences should be counted no matter how deeply they are nested.

(define syscount(lambda (n x)
  (if (empty? x)
    0
  (if (equal? n (car x))
    (+ 1 syscount(n (cdr x))))))) 

this is what i have written help me pls

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

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

发布评论

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

评论(3

倾城泪 2024-10-25 20:27:55
(define (syscount n x) 
            (if (null? x) 0 
                  (if (list? (car x))  (+ (syscount n (car x)) (syscount n (cdr x))) 
                         (+ (syscount n (cdr x)) (if (equal? n (car x)) 1 0))))) 

输出是

(系统计数'1'(1 2 3))
1
(系统计数'1'(1(1 2)3))
2
(系统计数'1'(1(1 2)1(1)3))
4

(define (syscount n x) 
            (if (null? x) 0 
                  (if (list? (car x))  (+ (syscount n (car x)) (syscount n (cdr x))) 
                         (+ (syscount n (cdr x)) (if (equal? n (car x)) 1 0))))) 

Output is

(syscount '1 '(1 2 3))
1
(syscount '1 '(1 (1 2) 3))
2
(syscount '1 '(1 (1 2) 1 (1) 3))
4

千里故人稀 2024-10-25 20:27:55

类似于:

(define (my-flatten xs)
  (foldr
   (lambda(x acc)
     (if (list? x)
         (append (my-flatten x) acc)
         (cons x acc)))
   (list)
   xs))

(define (my-filter pred xs)
  (let recur ((xs xs)
              (acc (list)))
    (if (empty? xs)
        (reverse acc)
        (if (pred (car xs))
            (recur (cdr xs) (cons (car xs) acc))
            (recur (cdr xs) acc)))))

(define (count-occur s ls)
  (let ((flatten-ls (my-flatten ls)))
    (foldl (lambda (e acc) (if (eq? s e)
                               (+ acc 1)
                               acc))
           0
           flatten-ls)))

测试:

> (count-occur 'foo (list 1 'foo (list 2 'foo 3 'bar) 4 (list 5 (list 6 'foo)) 7 'foo 8))
4

Something like:

(define (my-flatten xs)
  (foldr
   (lambda(x acc)
     (if (list? x)
         (append (my-flatten x) acc)
         (cons x acc)))
   (list)
   xs))

(define (my-filter pred xs)
  (let recur ((xs xs)
              (acc (list)))
    (if (empty? xs)
        (reverse acc)
        (if (pred (car xs))
            (recur (cdr xs) (cons (car xs) acc))
            (recur (cdr xs) acc)))))

(define (count-occur s ls)
  (let ((flatten-ls (my-flatten ls)))
    (foldl (lambda (e acc) (if (eq? s e)
                               (+ acc 1)
                               acc))
           0
           flatten-ls)))

Test:

> (count-occur 'foo (list 1 'foo (list 2 'foo 3 'bar) 4 (list 5 (list 6 'foo)) 7 'foo 8))
4
鹊巢 2024-10-25 20:27:55
(define (symcount n x)
  (cond((null? x) 0)
       ((list? (car x))(symcount n (car x)))
       ((eq? n (car x))(+ 1 (symcount n (cdr x))))
       (else(symcount n (cdr x)))))
(define (symcount n x)
  (cond((null? x) 0)
       ((list? (car x))(symcount n (car x)))
       ((eq? n (car x))(+ 1 (symcount n (cdr x))))
       (else(symcount n (cdr x)))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文