Question

using dr.racket programing language If we write a function that tests whether a list contains only...

using dr.racket programing language

If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write a generic function that can test lists with any predicate function if we provided it as one of the inputs.

Write a function list-o f-all? that takes as input a predicate function and a list and tests if all the elements of the list satisfy the input predicate. The function should behave as following:

> (list-o f-all? string? 0 (“a” “b” “42”))

#t

> (list-o f-all? number? 0 (1 2 3 “not-a-number”))

# f

> (list-o f-all? (lambda (n) (and (integer? n) (odd? n))) 0 (1 3 5))

#t

Please do not use the library function listo f for this question. Your solution should be general and should be able to test using any predicate function. Please do not hard code functions such as number? or even? in your implementation.

Homework Answers

Answer #1

Here is the complete Dr. Racket code for the given task. I have added comments for a better understanding of the same:

(define (list-of-all? p k l)

(if (null? l) #t ( ; if the list is empty returns true

if(p (car l)) (list-of-all? p (+ k 1) (cdr l)) #f

; Otherwise recurse

)))

You can comment below the answer in case of any doubts and I will be happy to help.

Please give a thumbs up if the answer could be of help!

All the best!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

ADVERTISEMENT