Question

In: Computer Science

RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...

RACKET

a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5)

b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given val. For example,

   (pair-sum? '(1 2 3) 3) ---> #t since 1+2=3. Similarly,

   (pair-sum? (gen-list 1 100) 1000) ---> #f since no two adjacent integers in the range 1 to 100 can sum to 1000.

You must use recursion, and not iteration. Please include explanation thanks.

Solutions

Expert Solution

Solution :-

a)

Previous Version:

//suppose start=n and end=N

void gen_list(int n, int N) // This gen_list will generate  list of consecutive integers, from start (n) to end (N)
{

   if(n>N) //Start >End

{

printf("The list is empty"); //It will print empty list

return; // return to main()

}
if (n <= N) //Start <End
{
printf("%d,", n); //print the number
n++; // increment n
gen_list(n,N); //recursion call
}
else
return; // return to main()
}

In RACKET:

(define (gen-list Start End)
(if (> Start End) // check if Start grearted than End or not
#f   
(cond ((<= Start End) // check if Start less than End or not
(gen-list Start (- End 1)) //Call to recursion
(display End) //show the output
(display " ")))))

b)

Previous Version:


int i=1, int j=1; //intilialize to variables

void pairsum?(int a[], int i, int j, int sum, int n) //list of integers are stored in the array a, sum represents sum of two numbers and n is the numbe of elements in the array
{

int k;

if(i>n || j>n) //required sum not exists

{

printf("#f");   

return; // return to main()

}

k=a[i]+a[j];

if(k==sum)

{

printf("#t");

return; // return to main()

}

else{

pairsum?(a,i+1,j+1,sum,n); //call to recursion after incrementing i and j
}
}

In RACKET:

(define (pair-sum? 1st s) // 1st is the input list of numbers and s is the required sum  
(if (null? (cdr lst)) // checking is list is null or not
#f
(if (= (+ (car lst) (car (cdr lst))) s) // checking the sum of paires is equal to s or not
#t
(pair-sum? (cdr lst) s)))) // Call to recursion

Thank you.... if any Quire's Feel free to ask me Sir  


Related Solutions

Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and...
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '('abc' 'ab' 'a')) should evaluate to '('ab''a') because these are the only strings shorter than 3.
Given two integers, start and end, where end is greater than start, write a recursive C++...
Given two integers, start and end, where end is greater than start, write a recursive C++ function that returns the sum of the integers from start through end, inclusive.Example: If start is 5 and end is 10 then the function will return: 45 which is sum of 5, 6, 7, 8, 9, and 10. int sum (int start, int end){
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to L except that every occurrence of x has been replaced with y. The following example illustrates the use of this function: > (subst 'c 'k '(c o c o n u t)) (k o k o n u t) Write a recursive Scheme function (all-different? L), which determines whether all elements of list L are distinct (that is, not equal?). The following example illustrates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT