WORK TOGETHER WITH EXPERTSMIND'S TUTOR TO ACHIEVE SUCCESS IN RECURSIVE FUNCTION ASSIGNMENT!
Recursive Function Assignment
Question 1. Write two versions, one non-tail-recursive and the other tail-recursive, of the following function.
The function takes as arguments an element x (of any type) and a non-negative integer n, and returns a list of n entries all identical to x. If the function is called with a negative n, an exception is thrown.
Solution:
Print "X"
Non tail Recursive:
let i= 0;;
print_string "type a number: " ;;
let num = read_int() in
if num>=0 then
while i<=num do
let i = i + 1 in
print_string("\n X");
done
else
print_string("Error");
Tail Recursive:
let i= 0;;
print_string "type a number: " ;;
let num = read_int() in
let rec pattern num=
if num>=0 then
if num==0 then
print_string("X \n");
else pattern (num-1);;
print_string("X\n");
ARE YOU LOOKING FOR RELIABLE RECURSIVE FUNCTION ASSIGNMENT HELP SERVICES? EXPERTSMINDS.COM IS RIGHT CHOICE AS YOUR STUDY PARTNER!
Question 2. Write two versions, one non-tail-recursive and the other tail-recursive, of the following function.
The function takes as argument a non-negative integer n, and returns the list of increasing positive integers up to n (empty list if n is 0). If the function is called with a negative n, an exception is thrown.
Solution: Print numbers from list
Non tail Recursive:
let i= 0;;
print_string "type a number: " ;;
let num = read_int() in
if num>=0 then
while i<=num do
let y = i + 1 in
print_int y;
print_string("\n");
done
else
print_string("Error");
Tail Recursive:
let i= 1;;
print_string "type a number: " ;;
let num = read_int() in
let rec printnonum=
if num>=0 then
if num==1 then
print_inti;
else print_int(iprintno(num-1);;
else
print_string("Error");
Question 3. Write two versions, one non-tail-recursive and the other tail-recursive, of the following function.
The function takes as argument a list, and returns the reversed list.
Solution: Reverse List
Non-tail Recursive:
let reverse list =
let a0, a1 = ([], 0), [] in
let aux0 (n, i) x =
(x, i mod 2) :: n, succi
in
let aux1 n = function
| l, 0 ->List.revl :: n
| l, _ ->l :: n
in
List.fold_left aux1 a1
@@ fst @@
List.fold_left aux0 a0 list
;;
reverse [1;2;3] ;
Tail Recursive:
let inverse list =
let rec a lt l =
Printf.printf "\nlist: [";
List.iter (fun e ->Printf.printf "%d; " e) lt;
Printf.printf"]\n";
match l with
| [] ->lt
|h::t -> a(h::lt) t
in a [] list ;;
inverse [0; 1; 2; 3; 4]
SAVE YOUR HIGHER GRADE WITH ACQUIRING RECURSIVE FUNCTION ASSIGNMENT HELP & QUALITY HOMEWORK WRITING SERVICES OF EXPERTSMINDS.COM
Question 4. Complete the following code for the function that takes an integer n as argument, and that returns the list of (prime) divisors for n.
Solution: Prime Factors
let divisors n =
if n< 2 then raise (Failure "Number must be > 1")
else
let rec aux helper n =
if n = 1 then []
else
if n mod helper then
helper :: aux helper (n / helper)
else aux(helper +1) n
in
aux 2 n
divisors 315;;
Question 5. Complete the following code for the function that takes an integer n as argument, and that returns true or false according to whether n is a prime number or not. Recall that a number p cannot divide n if p*p > n.
Solution: Prime no
let isprime n =
if n< 2 then raise (Failure "Number must be > 1")
else
let recnHasNoDivisor p = p*p > n || (n mod p <>nHasNoDivisor (p+1)) in
n<> 1 &&nHasNoDivisor2;;
valis_prime : int -> bool = <fun>
isprime7;;
DO YOU WANT TO EXCEL IN RECURSIVE FUNCTION ASSIGNMENT? HIRE TRUSTED TUTORS FROM EXPERTSMINDS AND ACHIEVE SUCCESS!