简体   繁体   English

我正在尝试在球拍(删除所有 xx elt)中编写一个函数,它返回一个新列表,其中删除了所有出现的 elt

[英]I'm trying to write a function in racket (delete-all xx elt) which returns a new list with all occurrences of elt removed

This is what i have but it only works if the elt appear at the start of the list这就是我所拥有的,但只有当 elt 出现在列表的开头时才有效

(define (delete-all xx elt)
  (cond ((null? xx) null)
        ((equal? elt (car xx)) (delete (cdr xx) elt))))

You're missing an extra case: what happens if the current element is not the one you want to delete?你错过了一个额外的案例:如果当前元素不是你想要删除的元素,会发生什么? Here's the general idea of what needs to be done, I'm not giving you a straight answer because this looks like homework (you should use the homework tag in your question).这是需要做什么的一般想法,我没有给你一个直接的答案,因为这看起来像家庭作业(你应该在你的问题中使用homework标签)。 Better fill-in the blanks yourself:最好自己填空:

(define (delete-all xx elt)
  (cond ((null? xx)            ; base case: empty list
         null)                 ; return the empty list
        ((equal? elt (car xx)) ; current element needs to be removed
         <???>)                ; ignore current element and make recursive call
        (else                  ; current element needs to be added to the list
         (<???> (car xx) <???>)))) ; add current element and make recursive call

Also, don't call delete in your answer, given that this is a recursive solution, you need to call delete-all instead, but with appropriate arguments to keep the recursion going until the base case is reached.另外,不要在您的答案中调用delete ,因为这是一个递归解决方案,您需要调用delete-all ,但使用适当的参数来保持递归直到达到基本情况。 Hint : what about cons and cdr ?提示conscdr怎么样?

You could also use filter , that is if you're allowed to use higher order functions :您也可以使用filter ,也就是说,如果您可以使用高阶函数

(define (delete-all xx elt)
  (filter (lambda (y) (not(eq? xx y))) elt))
(define (delete-all xx elt)
  remove* xx elt)

暂无
暂无

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

相关问题 我将如何在 Racket 博士中编写一个 function 消耗一个数字列表并生成一个已映射的新数字列表? - How would I write a function in Dr. Racket which consumes a list of numbers and produces a new list of numbers which have been mapped? 如何在Haskell中编写一个函数,该函数接受一个I​​nts列表并返回该列表的所有连续子列表? - How can I write a function in Haskell that takes a list of Ints and returns all the contiguous sublists of that list? 在Racket中,如何返回包含两个不同字典中出现的所有键的列表 - In Racket how do I return a list that contains all the keys which occur in two different dictionaries 我将如何在 Dr. Racket 中编写一个 function,它使用 2 个可能的符号列表并将它们替换为其他符号? - How would I write a function in Dr. Racket which consumes a list of 2 possible symbols and replaces them with something else? 如何删除球拍中第二个列表中的所有元素? - How can I remove all the elements that are in the second list in racket? 我正在尝试在 python 列表上写一个条件 - I'm trying to write a condition on the python list Scheme函数-获取一个原子和一个列表,然后返回新列表,其中从给定列表中删除了给定的原子出现 - Scheme function - take an atom and a list and return new list where the given atom occurrences are removed from given list Python:给定一个用户列表,我需要编写一个函数,该函数在一个字符串中返回所有用户名和角色,每个值都标记为 - Python: Given a list of users, I need to write a function, that returns all of user's names and roles in a string with each value labeled 我该如何安排这个 function 所以它返回一个包含所有这些列表的列表 - How can I arrange this function so it returns a list with all this lists on it 试图创建一个函数,它接受一个列表并返回与其第一个元素相同的所有元素的列表,但我的函数不起作用 - Trying to create a function that takes a list and returns a list of all of all elements identical to its first element, but my function isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM