Reputation: 1455
This procedure takes in a list of booleans, if it sees a false, it should terminate and return false. However the way it's running it's always true.
(define L3 (cons true (cons false empty)))
(check-expect (all-true? L3) false)
(define (all-true? lob)
(cond [(empty? lob) true]
[else
(if (boolean? (first lob))
(all-true? (rest lob))
false)]))
I understand that i could have used an AND clause after the else statement also.
Upvotes: 0
Views: 58
Reputation: 236112
This should fix the problems:
(define (all-true? lob)
(cond [(empty? lob) true]
[(first lob) (all-true? (rest lob))]
[else false]))
Explanation:
cond
instead of writing an if
inside the else
part of a cond
.boolean?
, that's for checking whether a value is boolean, not for determining if it's true.Alternatively, in Racket we can use andmap
for the same effect:
(define (all-true? lob)
(andmap identity lob))
In other interpreters, we can import every
from SRFI-1:
(require srfi/1)
(define (all-true? lob)
(every (lambda (x) x) lob))
Upvotes: 3