Question

Create a function that returns a boolean value if all-elements are not odd or even. Using...

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)?

Homework Answers

Answer #1

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

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
Similar Questions
(1)Create a LISP function FLATTEN, a function that returns all the elements of an arbitrarily nested...
(1)Create a LISP function FLATTEN, a function that returns all the elements of an arbitrarily nested list in a single-level list. (FLATTEN ’((A B (R)) A C (A D ((A (B)) R) A))) should return (A B R A C A D A B R A). (2) Create a Lisp function EXP-EVAL, a function that evaluates an arithmetic expression. You may assume that the binary operators used for an arithmetic expression are: +, -, *, and /, and each of...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As...
Printing the value in the function, modify your function so that it returns a value of...
Printing the value in the function, modify your function so that it returns a value of true or false. Do not print anything from inside function itself. Call your function and have the return result of the function (Boolean True or False). Do not create a string called "True" or "False". Do not create a string called "Equal" or "Not Equal" here. Actually return a boolean. Create a variable in your main code outside of the is_equal function store the...
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...
Using the count function, create a function that returns only the unique elements in a list....
Using the count function, create a function that returns only the unique elements in a list. Now try to do the same thing using the set function instead of count. For example: f([1,2,2,3]) gives back [1,2,3].
In Python: Problem 5] Write a function that accepts a list as argument and returns a...
In Python: Problem 5] Write a function that accepts a list as argument and returns a list that contains: (1) The positive elements of the taken list (2) The negative elements of the taken list (3) The odd elements of the taken list (4) The even elements of the taken list
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
In c++ Implement the following recursive function, that returns true if all elements in the given...
In c++ Implement the following recursive function, that returns true if all elements in the given array are smaller than the given value, otherwise false. Return false for the empty array and NULL array cases. bool all_smaller_r(int *a1, int size, int value) { }
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT