Question

Write these two functions in Scheme: even1? odd1? Each accepts a number as input and returns #t o...

Write these two functions in Scheme:

even1?

odd1?

Each accepts a number as input and returns #t or #f if the number meets the condition.

The functions are named with a trailing 1 as Scheme already defines even? and odd?.

You can check your answer with this simple test:

(equal? (even1? 2) #t)

(equal? (even1? 1) #f)

(equal? (odd1? 1) #t)

(equal? (odd1? 2) #f)

(equal (square 2) 4)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

So I am writing the 'even1?' and 'odd1?' function.

I have written the code in the block below.

I have added the comments to explain the work of both the functions accurately.

In the fourth test case given above, the correct statement should be - (equal? (square 2) 4)

So the final test case is

(equal? (even1? 2) #t)

(equal? (even1? 1) #f)

(equal? (odd1? 1) #t)

(equal? (odd1? 2) #f)

(equal (square 2) 4)

And the results are

true

false

true

false

CODE

; This is the code to find if the number given in input is even of not. it displays 'true' if the number is even and 'false' if the number is odd


(define (even1? n)
(cond
[(equal? (modulo n 2) 0) (display "true\n")])
(cond
[(equal? (modulo n 2) 1) (display "false\n")])
)

; This is the code to find if the number given in input is odd of not. it displays 'true' if the number is odd and 'false' if the number is even


(define (odd1? n)
(cond
[(equal? (modulo n 2) 0) (display "false\n")])
(cond
[(equal? (modulo n 2) 1) (display "true\n")])
)


(equal? (even1? 1) #f)
(equal? (odd1? 1) #t)
(equal? (odd1? 2) #f)
(equal? (square 2) 4)

Add a comment
Know the answer?
Add Answer to:
Write these two functions in Scheme: even1? odd1? Each accepts a number as input and returns #t o...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • 2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new...

    2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new list that contains only the even numbers. (Hint: use reminder function) Sample runs: (get-even-nums ‘(1 2 3 4 5 6 7 8 9)) should return (8 6 4 2). (get-even-nums ‘(1 3 5 7 9)) should return ’( ).

  • You must write each of the following scheme functions. You must use only basic scheme functions...

    You must write each of the following scheme functions. You must use only basic scheme functions (those described in class), do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (key-store L1 KEY) that accepts a list of two-tuples L1 and a value KEY. This function returns the second value of the first tuple that begins with KEY....

  • Write the function below in scheme/lisp programming language. Drracket Exercise: It is well known that n^2...

    Write the function below in scheme/lisp programming language. Drracket Exercise: It is well known that n^2 is equal to the sum of the first n odd numbers. For example, 16 = 4^2 = 7 + 5 + 3 + 1. Write a function that takes as input a natural number, n, and that returns the square of its input by adding the first n odd numbers. Your code may not contain delayed operations and must use accumulative recursion.

  • PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI()...

    PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI() This function prints out your name and lists any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and prints out a message indicating if the number is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one...

  • You must write each of the following scheme functions. You must use only basic scheme functions...

    You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s"...

    In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s" is an object of the type "string" and "pos" is a boolean value with the default argument of "true". If "pos" is "true", the function returns the summation of the ASCII codes of all the characters located at the even indices of "s" and if "pos" is "false", the function returns the summation of the ASCII codes of all the characters located at the...

  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT