Question

Write recursive Dr Racket functions to do the following. The functions should work on all types...

Write recursive Dr Racket functions to do the following. The functions should work on all types of lists( not just simple lists of atoms). Document (via direct Dr Racket screens/outputs) both your code and the results of running the test cases.

a) Count how many times an item (atom) appears on a list.

Minimum test cases:

(count 3 '(1 5 6 7 8 3 2 3 )) returns 2

(count dog '(cat bird cat dog frog)) returns 1

(count a '(a(a b ) (c (b c)) ((a))b)) returns 3

(count 3 '(1)) returns 0

(count none '()) returns 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int binSearch(int[] a, int key) {
   return binSearch0(a, key, 0, a.length - 1);
}
public static int binSearch0(int[] a, int key, int from, int to) 
{ 
public static int binSearch(int[] a, int key) {
   return binSearch0(a, key, 0, a.length - 1);
}
public static int binSearch0(int[] a, int key, int from, int to) {
    if (from > to) return -1;
    int mid = from + (to - from) / 2;
    if (key < a[mid]) 
    return binSearch0(a, key, from, mid - 1);
    else if (key < a[mid]) 
    return binSearch0(a, key, mid + 1, to);
    else return mid;
 }
   if (from > to) return -1;
   int mid = from + (to - from) / 2;
    if (key < a[mid]) 
    return binSearch0(a, key, from, mid - 1);
    else if (key < a[mid]) 
    return binSearch0(a, key, mid + 1, to);
    else return mid;
 }
Add a comment
Know the answer?
Add Answer to:
Write recursive Dr Racket functions to do the following. The functions should work on all types...
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
  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • Write a Python file containing the following functions. Also turn in the output from testing the...

    Write a Python file containing the following functions. Also turn in the output from testing the functions. All arguments to the functions may be hard-coded. Function 1 takes a list of strings as a parameter and returns a list of strings consisting of all the strings in the original list that have identical consecutive letters. For example: fun1 ([llama, good, cat, abba, abab, 112, dog]) returns [llama, good, abba, 112] Function 2 takes an integer (n), representing the number of...

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or...

    Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or LISP if you prefer) . A function (binomial N k) that returns the binomial coefficients C(N, k), defined recursively as: C(NO) = 1, C(N, N) = 1, and, for 0<k < N E(N, k) = C(N-1, k) + C(N-1, k-1) 2. A function (mod N M) that returns the modulus remainder when dividing N by M 3. A function (binaryToDecimal b) that takes a...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • 2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn<2 else f(n-1) + f(n-2) end If m=0 else B. function g(m...

    2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn<2 else f(n-1) + f(n-2) end If m=0 else B. function g(m,n) g(m-1,m'n) C. function h(n) if n 100 else 3 h(n+5) end D. function j(m.n) IT m=n 100 j(m-n,n) 10 j(n,n-m) elseif mn else 2. Which of the following recursive functions, written in a fictitious language, are tail recursive? Select all that are A. function f(n) ifn

  • Write a class called StringCode with the following functions. (Do not change these function names or...

    Write a class called StringCode with the following functions. (Do not change these function names or return types, else tests will fail). public static String blowup (String str) Returns a version of the original string as follows: each digit 0-9 that appears in the original string is replaced by that many occurrences of the character to the right of the digit. So the string "a3tx2z" yields "attttxzzz" and "12x" yields "2xxx". A digit not followed by a character (i.e. at...

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