简体   繁体   English

球拍定义列表

[英]Racket define list

I have to solve nest task using language Racket.我必须使用语言球拍解决嵌套任务。 By given list I have to create new list holding only elements that division by 10 has no leftover.根据给定的列表,我必须创建仅包含除以 10 没有剩余的元素的新列表。

My code so far:到目前为止我的代码:

(define (brel x sp)
   (cond ((null? sp) 0)
         (( = (remainder (car sp) 10) 0) (car sp))
         (else (brel x (cdr sp)))))

(define (spbr L)
  (define (f l1)
    (if (null? l1) '()
     (cons (brel (car l1) L) (f (cdr l1)))))
 (f L))
(spbr (list 50 5 3))

Give code currently count every repeats of elements in first list and add them in new one.给代码当前计算第一个列表中元素的每个重复并将它们添加到新列表中。 What must I change to make it works?我必须改变什么才能使它工作?

You don't need a helper procedure, just build a new list with only the items that meet the condition:您不需要帮助程序,只需构建一个仅包含满足条件的项目的新列表:

(define (spbr L)
  (cond ((null? L) '())
        ((= (remainder (car L) 10) 0)
         (cons (car L) (spbr (cdr L))))
        (else
         (spbr (cdr L)))))

Using the filter procedure would be more idiomatic:使用filter程序会更惯用:

(define (spbr L)
  (filter (lambda (e) (zero? (remainder e 10)))
          L))

Either way, it works as expected:无论哪种方式,它都按预期工作:

(spbr '(50 5 3 70))
=> '(50 70)

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

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