简体   繁体   中英

In Dr. Racket, how would I write a racket function which produces true if every integer in a list is even and false otherwise?

In Dr. Racket, how would I write a racket function which produces true if every integer in a list is even and false otherwise. This is my code so far:

(define (everything-is-even? speciallist)
  (cond
    [(even? (first speciallist)) true]
    [(even? (first speciallist)) (everything-is-even? (rest speciallist))]
    [(empty? speciallist) true]
    [else false]))

If for example, I type

(everything-is-even? (cons 2 (cons 6 (cons 9 empty))))

My desired output is false, since 9 is odd. However, instead I get a true. Any hints on how to solve this code will be very helpful.

You can use andmap .

I not sure you only want to check all number are even number or you want check every element are even number or false .

If you want check all number are even number or false .

#lang racket
(define (everything-pass-test lon)
  (local [(define (pass? x)
            (if (number? x)
                (even? x)
                (false? x)))]
    (andmap pass? lon)))

(everything-pass-test '(2 4 6 8 10 #t)) ; #f
(everything-pass-test '(2 4 6 8 10 9)) ; #f

If you want check all number are even number.

#lang racket
(define (everything-pass-test lon)
  (andmap even? lon))

(everything-pass-test '(2 4 6 8 10)) ; #t
(everything-pass-test '(2 4 6 8 10 9)) ; #f

Or use this way.

(define (all-enve? lon)
  (cond
    [(empty? lon) #t]
    [(even? (first lon))
     (all-enve? (rest lon))]
    [else #f]))


(all-enve? '(2 4 6 8 10)) ; #t 
(all-enve? '(2 4 1 6 8 10)) ; #f

Or you can use this way it looks like andmap .

(define (everything-pass-test-v2 lon)
  (local [(define (my-and a b) (and a b))]
    (foldr my-and #t (map even? lon))))

(everything-pass-test-v2 '(2 4 6 8 10)) ; #t 
(everything-pass-test-v2 '(2 4 6 8 10 9)) ; #f

If you need to check that every number in the list is even you cannot have true as the result of the first number being even. It will stop there since you have decided the result is true .

What you know is that the only time you know the result is true is when you hit the end of the list. eg.

(everything-is-even? '()) ; ==> #t

You also know that if you get to the next cond clause you do not have an empty list. If so the first element is not even you know the result is #f and need not investigate the list further.

If neither of the previous cond clauses hit you should have a else clause that checks everything-is-even? for the rest of the list.

Now you can switch the last two such that if the first element is event you recurse and then have an elsea with #f`, but you need to have the test for empty list first since when you have it on the third term like in your code you are already peeking in the first element of something that might not have a first element.

Here are some examples:

(everything-is-even? '())    ; ==> #t (base case)
(everything-is-even? '(3))   ; ==> #f (base case)
(everything-is-even? '(2))   ; ==> #t (default case leading to (everything-is-even? '()))
(everything-is-even? '(2 3)) ; ==> #f (default case leading to (everything-is-even? '(3)))

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