Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 名单程序(计划)_List_Scheme_Procedure - Fatal编程技术网

List 名单程序(计划)

List 名单程序(计划),list,scheme,procedure,List,Scheme,Procedure,我正在写一个程序,返回一个包含所有负奇数和正奇数的列表 通过在原语过滤过程中使用lambda,甚至删除了整数(字符串可以保留)。我也在避免使用递归,但这正是困扰我的问题。 到目前为止,我得到的是: (define (f2b lst) (cond ((null? lst)'()) ; if the list is empty, return the empty list ((pair? (car lst)) ; if the current element isn't

我正在写一个程序,返回一个包含所有负奇数和正奇数的列表 通过在原语过滤过程中使用lambda,甚至删除了整数(字符串可以保留)。我也在避免使用递归,但这正是困扰我的问题。 到目前为止,我得到的是:

(define (f2b lst)
    (cond ((null? lst)'()) ; if the list is empty, return the empty list
          ((pair? (car lst))  ; if the current element isn't a list   
              (filter (lambda (x) (or (even? x) (positive? x))) (car lst))
              (filter (lambda (x) (or (odd?  x) (negative? x))) (car lst))) 

 (else (string? (car lst))  ;otherwise, if the current element is a string,
            (car lst)       ; then return that element         
            (f2b (cdr lst))))) 

我也不确定如何同时应用这两个过滤过程。

比这简单多了。你所要做的就是过滤列表。您只需要适当的谓词

您希望何时保留一个元素?你是根据你想删除的内容来表达的,所以让我们从这个开始。如果它是一个负奇数或正偶数整数,则要删除它,并保留所有其他内容。更容易将其分解为更小的函数

(define (positive-even? x) (and (positive? x) (even? x)))
(define (negative-odd? x) (and (negative? x) (odd? x)))
(define (remove-num? x) (or (positive-even? x) (negative-odd? x)))
这定义了是否保留一个数字。但是list元素可能不是数字。所以我们 如果不是数字,或者不匹配,请保留它。
remove num?

(define (keep-element? x) (or (not (number? x)) (not (remove-num? x))
然后,您的函数只需调用过滤器:

(define (f2b lst) (filter keep-element? lst))
似乎有效:

(f2b '(-4 -3 -2 -1 0 1 2 3 4 "a string" "another"))   
=> (-4 -2 0 1 3 "a string" "another")

下面是它作为一个大功能的外观:

(define (f2b lst)
  (filter
   (lambda (x)
    (or (not (number? x)) 
        (not (or (and (positive? x) (even? x))
                 (and (negative? x) (odd? x))))))
   lst)
就我个人而言,嵌套的
or not or and
对我来说有点难读懂


好的,显然你有嵌套列表。在这里,您所要做的就是映射
过滤器的结果,该过滤器具有以下功能:

  • 给定列表时,返回
    (f2b lst)
  • 否则,返回未更改的元素

我将把它作为一个练习留给你们,因为,如果你们认为我的函数可以在嵌套列表上工作,显然你们有很多要学习的东西

我试着运行这段代码,但它只是运行了我的测试用例并打印了整个列表。编辑:我应该澄清一下,我是通过嵌套列表运行它的。@user2789945:您运行了我给出的最后一个函数吗?你给它的输入是什么?是的,它对常规列表有效,但当我通过嵌套列表传递它时,它打印了整个列表。@user2789945:哦,也许你应该提到你在问题中有嵌套列表?