Dr. Racket Recursion 计数出现次数 [英] Dr. Racket Recursion count occurrences

查看:39
本文介绍了Dr. Racket Recursion 计数出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Racket 的新手,正在努力学习.我正在解决一些我正在努力解决的问题.这是问题所在:

I'm new to Racket and trying to learn it. I'm working through some problems that I'm struggling with. Here is what the problem is asking:

写一个递归函数出现的定义,它接受一个数据表达式 a 和一个列表 s,并返回数据表达式 a 在列表 s 中出现的次数.

Write a definition for the recursive function occur that takes a data expression a and a list s and returns the number of times that the data expression a appears in the list s.

示例:

(occur '() '(1 () 2 () () 3)) =>3

(occur 1 '(1 2 1 ((3 1)) 4 1)) => 3(注意它只查看列表中的整个元素)

(occur 1 '(1 2 1 ((3 1)) 4 1)) => 3 (note that it only looks at whole elements in the list)

(occur '((2)) '(1 ((2)) 3)) => 1

这是我目前所写的:

 (define occur
      (lambda (a s)
        (cond
          ((equal? a (first s))
          (else (occur a(rest s))))))

我不确定如何实现计数.下一个问题是类似的,我不知道如何解决这个问题.这个问题是这样说的:

I'm not sure how to implement the count. The next problem is similar and I have no idea how to approach that. Here is what this problem says:

(这类似于上面的函数,但它也查看子列表内部)编写一个递归函数 atom-occur?,它接受两个输入,一个原子 a 和一个列表 s,并输出布尔值 true if and仅当 a 出现在 s 中的某处时,或者作为 s 中的数据表达式之一,或者作为 s 中的数据表达式之一中的数据表达式之一,或者……等等.

(This is similar to the function above, but it looks inside the sublists as well) Write a recursive function atom-occur?, which takes two inputs, an atom a and a list s, and outputs the Boolean true if and only if a appears somewhere within s, either as one of the data expressions in s, or as one of the data expression in one of the data expression in s, or…, and so on.

示例:

(atom-occur? 'a '((x y (p q (a b) r)) z)) => #t

(atom-occur? 'm '(x (y p (1 a (b 4)) z))) => #f

任何帮助将不胜感激.谢谢.

Any assistance would be appreciated. Thank you.

推荐答案

在 Racket 中,解决这个问题的标准方法是使用内置程序:

In Racket, the standard way to solve this problem would be to use built-in procedures:

(define occur
  (lambda (a s)
    (count (curry equal? a) s)))

当然,您想从头开始实现它.不要忘记基本情况(空列表),并记住在找到新匹配项时添加一个单元.试试这个:

But of course, you want to implement it from scratch. Don't forget the base case (empty list), and remember to add one unit whenever a new match is found. Try this:

(define occur
  (lambda (a s)
    (cond
      ((empty? s) 0)
      ((equal? a (first s))
       (add1 (occur a (rest s))))
      (else  (occur a (rest s))))))

第二个问题类似,但它使用标准模板来遍历列表列表,我们继续对firstrest 进行递归输入列表,并且仅在我们处于原子中时测试相等性:

The second problem is similar, but it uses the standard template for traversing a list of lists, where we go down on the recursion on both the first and the rest of the input list, and only test for equality when we're in an atom:

(define atom-occur?
  (lambda (a s)
    (cond
      ((empty? s) #f)
      ((not (pair? s))
       (equal? a s))
      (else (or (atom-occur? a (first s))
                (atom-occur? a (rest s)))))))

这篇关于Dr. Racket Recursion 计数出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆