简体   繁体   中英

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.

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:

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

Either way, it works as expected:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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