简体   繁体   English

Racket 中的递归/列表

[英]Recursion/Lists in Racket

I am trying to make the function remove-member , where I have a list of strings and I give one of the members of the string (ie "A" ) to the function, and it removes that member and returns the rest of the list without that member.我正在尝试创建函数remove-member ,其中我有一个字符串列表,我将字符串的成员之一(即"A" )提供给函数,它会删除该成员并返回列表的其余部分没有那个成员。 At the moment I have created tests and started my function, but I am lost beyond that.目前我已经创建了测试并开始了我的功能,但我已经迷失了。 I know recursion functions include the first member of the list and the rest of it, but how would I remove the first member of a string?我知道递归函数包括列表的第一个成员和它的其余部分,但我将如何删除字符串的第一个成员?

(define str (list->string (list 'A 'B 'C)))

(check-expect (remove-occurrences "A") (list 'B 'C))
(check-expect (remvove-occurrences '()) (list 'A 'B 'C))

(define (remove-occurrences r)
  (cond
    [(empty? r) str]
    [(??? r)]))

To remove a single element from a list:要从列表中删除单个元素:

  1. Is the list empty?列表是空的吗? If it is, we're done and the result is the empty list.如果是,我们就完成了,结果是空列表。
  2. OK, it's not empty.好的,它不是空的。 Is the first element of the list the same as the element you want to remove?列表的第一个元素是否与您要删除的元素相同? If it is then the result is the rest of the list.如果是,则结果是列表的其余部分。
  3. OK, it's not empty, and the first element didn't match.好的,它不为空,并且第一个元素不匹配。 So the answer is a list consisting of a cons of the first element and the result of removing the element from the rest of the list.所以答案是一个列表,由第一个元素的 cons 和从列表的其余部分中删除元素的结果组成。 Which you now know how to do.你现在知道该怎么做了。

Alternatively, to remove all occurrences of an element from a list:或者,要从列表中删除所有出现的元素:

  1. Is the list empty?列表是空的吗? If it is, we're done and the result is the empty list.如果是,我们就完成了,结果是空列表。
  2. OK, it's not empty.好的,它不是空的。 Is the first element of the list the same as the element you want to remove?列表的第一个元素是否与您要删除的元素相同? If it is then the result is the the result of removing all occurrences of the element from rest of the list, which you know how do to now.如果是,那么结果就是从列表的其余部分中删除所有出现的元素的结果,您现在知道该怎么做。
  3. OK, it's not empty, and the first element didn't match.好的,它不为空,并且第一个元素不匹配。 So the answer is a list consisting of a cons of the first element and the result of removing the element from the rest of the list.所以答案是一个列表,由第一个元素的 cons 和从列表的其余部分中删除元素的结果组成。 Which you now know how to do.你现在知道该怎么做了。

How these functions differ:这些功能有何不同:

> (remove-one '(a b b c) 'b)
'(a b c)
> (remove-all '(a b b c) 'b)
'(a c)

Let's follow the data type.让我们按照数据类型。

This way we get the mundane tasks taken care of automatically for us, and get to focus our creative mental abilities on more interesting, less common aspects of our problem:通过这种方式,我们可以自动为我们处理平凡的任务,并将我们的创造性思维能力集中在我们问题中更有趣、更不常见的方面:

(define (list-p ad)
  (cond
    ((null? ad)       ; follow
      #t)
    ((pair? ad)
      (let ((a (car ad))  ; the
            (d (cdr ad))) ; type! 
        (list-p d)))
    (else #f)))

We can see this predicate as a constructor of Boolean values.我们可以将此谓词视为布尔值的构造函数。 Creating a list following the same example aka skeleton code is also easy:按照相同的示例(即骨架代码)创建列表也很容易:

(define (list-l ad)
  (cond
    ((null? ad)  ; return proper
      (list))    ; type here, and
    ((pair? ad)
      (let ((a (car ad))
            (d (cdr ad))) 
        (list-l d)))   ; here
    (else #f)))

We've just followed the type's skeleton code, mended it in the few appropriate places, and got ourselves a working code which creates values of the proper type, all by itself!我们刚刚遵循了该类型的骨架代码,在几个适当的地方对其进行了修改,并得到了一个可以自己创建正确类型值的工作代码!

But does it create any interesting value, fully using the supplied argument?但它是否创造了任何有趣的价值,完全使用提供的参数? Evidently, not.显然,不是。 For that we must mend the recursive call:为此,我们必须修改递归调用:

(define (list-copy ad)
  (cond
    ((null? ad)  
      (list))    
    ((pair? ad)
      (let ((a (car ad))
            (d (cdr ad))) 
        (cons ...         ; here
              (list-copy d))
           ))
    (else #f)))

Now you get to tweak this further, consing the first element of a pair conditionally , as required by your problem.现在您可以进一步调整它,根据您的问题要求有条件地使用 pair 的第一个元素。

(note: null? could be called empty? in your dialect). (注意: null?在你的方言中可以称为empty? )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM