Create a function that returns a boolean value if all-elements are not odd or even.
Using Dr. Racket and the How to Design a Function (HtDF ) recipe, create a function that will return the following check-expect:
(check-expect (all-elements? even? (list 1 2 3) false)
(check-expect (all-elements? even? (list 2 4 6) true)
(check-expect (all-elements? odd? (list 1 3 5) true)
Can you find an element in the list where the predicate fails (return false)?
Using Dr. Racket we can return a list containing(in order)the items of a givenlist for which a given function returns true, example
>(filter even?(list 1 2 3 4))
-(listof number)
'(2 4)
>(filter odd?(list 1 2 3 4))
-(listof number )
'(1 3)
similarly we can apply for the false condition and the above question answer is below:-
(define (all elements ?predicate Ist)
(cond ((empty? lst)true)
((predicate(first Ist)) (all-elements?predicate(cdr Ist)))
(else false)))
DIFFERENT APPROACH
;; make 'and' as a function
(define(my-and a b) (and a b))
(define(all-elements ?predicate args)
(foldr my-and #t (map predicate args)))
(all-elements? odd '(1 3 5))
;;#t
or as a variadic function:
(define(all-elements?predicate.args)
(foldr my-and(map predicate args)))
then one can type :
(all-elements? odd? 1 3 5 7 9);;#t
Get Answers For Free
Most questions answered within 1 hours.