OCaml Code: Below is streams.ml and it needs the value "head". Please fix the code and make sure to test the code using various test cases to make sure the code works.  streams.ml let rec even n =   if n = 0 then true   else odd (n - 1) and odd n =   if n = 0 then false   else even (n - 1) let rec squares n =   let rec squares_aux m acc =     if m = 0 then List.rev acc     else squares_aux (m - 1) (acc @ [m * m])   in   squares_aux n []    let rec fibs n =   let rec fibs_aux a b count acc =     if count = 0 then List.rev acc     else fibs_aux b (a + b) (count - 1) (a :: acc)   in

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

OCaml Code: Below is streams.ml and it needs the value "head". Please fix the code and make sure to test the code using various test cases to make sure the code works. 

streams.ml

let rec even n =
  if n = 0 then true
  else odd (n - 1)
and odd n =
  if n = 0 then false
  else even (n - 1)

let rec squares n =
  let rec squares_aux m acc =
    if m = 0 then List.rev acc
    else squares_aux (m - 1) (acc @ [m * m])
  in
  squares_aux n []
  

let rec fibs n =
  let rec fibs_aux a b count acc =
    if count = 0 then List.rev acc
    else fibs_aux b (a + b) (count - 1) (a :: acc)
  in
  fibs_aux 0 1 n []

let rec evenFibs n =
  let rec fibs_aux a b count acc =
    if count = 0 then List.rev acc
    else if a mod 2 = 0 then fibs_aux b (a + b) (count - 1) (a :: acc)
    else fibs_aux b (a + b) count acc
  in
  fibs_aux 0 1 n []

let rec oddFibs n =
  let rec fibs_aux a b count acc =
    if count = 0 then List.rev acc
    else if a mod 2 = 1 then fibs_aux b (a + b) (count - 1) (a :: acc)
    else fibs_aux b (a + b) count acc
  in
  fibs_aux 1 1 n []

let rec is_prime n =
  let rec is_not_divisible k =
    k * k > n || (n mod k <> 0 && is_not_divisible (k + 1))
  in
  n > 1 && is_not_divisible 2

let rec primes n =
  let rec find_primes k count acc =
    if count = 0 then List.rev acc
    else if is_prime k then find_primes (k + 1) (count - 1) (k :: acc)
    else find_primes (k + 1) count acc
  in
  find_primes 2 n []

let rec rev_zip_diff l1 l2 f =
  match l1, l2 with
  | [], _ | _, [] -> []
  | x :: xs, y :: ys -> (x, y, f (x, y)) :: rev_zip_diff xs ys f

let rec printGenList lst f =
  match lst with
  | [] -> ()
  | x :: xs -> f x; printGenList xs f

let rec printList lst filename =
  let oc = open_out filename in
  let rec print_list_helper lst =
    match lst with
    | [] -> close_out oc
    | x :: xs -> output_string oc (string_of_int x ^ " "); print_list_helper xs
  in
  print_list_helper lst

let rec printPairList lst filename =
  let oc = open_out filename in
  let rec print_pair_list_helper lst =
    match lst with
    | [] -> close_out oc
    | (x, y) :: xs ->
        output_string oc ("(" ^ string_of_int x ^ ", " ^ string_of_int y ^ ") ");
        print_pair_list_helper xs
  in
  print_pair_list_helper lst

let rec take n lst =
  if n <= 0 then []
  else
    match lst with
    | [] -> []
    | x :: xs -> x :: take (n - 1) xs

let string_of_tuple_list lst =
  "[" ^ String.concat "; " (List.map (fun (x, y, z) -> "(" ^ string_of_int x ^ ", " ^ string_of_int y ^ ", " ^
                                                      string_of_int z ^ ")") lst) ^ "]"

let () =
  let string_of_int_list lst = "[" ^ String.concat "; " (List.map string_of_int lst) ^ "]" in

  print_endline ("even 2;;\n- : bool = " ^ string_of_bool (even 2));
  print_endline ("even 3;;\n- : bool = " ^ string_of_bool (even 3));
  print_endline ("odd 2;;\n- : bool = " ^ string_of_bool (odd 2));
  print_endline ("odd 3;;\n- : bool = " ^ string_of_bool (odd 3));
  print_endline ("take 10 squares;;\n- : int list = " ^ string_of_int_list (take 10 (squares 10)));
  print_endline ("take 10 fibs;;\n- : int list = " ^ string_of_int_list (take 10 (fibs 10)));
  print_endline ("take 10 evenFibs;;\n- : int list = " ^ string_of_int_list (take 10 (evenFibs 10)));
  print_endline ("take 10 oddFibs;;\n- : int list = " ^ string_of_int_list (take 10 (oddFibs 10)));
  print_endline ("take 10 primes;;\n- : int list = " ^ string_of_int_list (take 10 (primes 10)));
  print_endline ("take 5 (rev_zip_diff evenFibs oddFibs (fun (x,y)->x-y));;\n- : (int * int * int) list = " ^
                string_of_tuple_list (take 5 (rev_zip_diff (evenFibs 10) (oddFibs 10) (fun (x, y) -> x - y))));
  print_endline "printGenList [\"how\"; \"the\"; \"turntables\"] (fun s -> print_string (s ^ \" \"));;";
  printGenList ["how"; "the"; "turntables"] (fun s -> print_string (s ^ " "));
  print_endline "\n-: unit = ()";
  print_endline "printList [2; 4; 6; 8] \"printList.txt\";; (* \"2 4 6 8 \" *)";
  printList [2; 4; 6; 8] "printList.txt";
  print_endline "printPairList [(2, 1); (3, 2); (4, 3)] \"printPairList.txt\";; (* \"(2, 1) (3, 2) (4, 3) \" *)";
  printPairList [(2, 1); (3, 2); (4, 3)] "printPairList.txt"
 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I ran the code and still got the same error. The code still needs the value head. Please fix the code and show screenshots of the correct code with the output. Make sure to use various test cases to test the code to make sure it works. Attached are images of the directions. 

list followed by a string. The string is the name of the output file that you will write the list
to. This function should leverage printGenList and provide an anonymous function (the fun ...
->... construct) that will do the appropriate pretty printing to the output file. This anonymous
function should print the element of the list and then a space character. printList should have the
following type signature:
val printList: int list -> string -> unit
Create a function called printPairList that will pretty print a list consisting of integer pairs.
The function will take an (int *int) list followed by a string. The string is the name of the
output file that you will write the list to. printPairList should leverage print GenList and
provide an anonymous function (the fun ->... construct) that will do the appropriate
pretty printing. This anonymous function should print an open parenthesis, the first element of
the pair, a comma, a space, the second element of the pair, and then a close parenthesis followed
by a space. printPairList should have the following type signature:
val printPairList : (int int) list -> string -> unit
Transcribed Image Text:list followed by a string. The string is the name of the output file that you will write the list to. This function should leverage printGenList and provide an anonymous function (the fun ... ->... construct) that will do the appropriate pretty printing to the output file. This anonymous function should print the element of the list and then a space character. printList should have the following type signature: val printList: int list -> string -> unit Create a function called printPairList that will pretty print a list consisting of integer pairs. The function will take an (int *int) list followed by a string. The string is the name of the output file that you will write the list to. printPairList should leverage print GenList and provide an anonymous function (the fun ->... construct) that will do the appropriate pretty printing. This anonymous function should print an open parenthesis, the first element of the pair, a comma, a space, the second element of the pair, and then a close parenthesis followed by a space. printPairList should have the following type signature: val printPairList : (int int) list -> string -> unit
To start, write a function called even which returns true if the integer argument is even, false oth-
erwise. Write a function called odd which returns true if the integer argument is odd, false otherwise.
Now create five streams: squares, fibs, evenFibs, oddFibs, and primes. squares contains the
sequence of perfect squares starting from 1. You may find the map function helpful to create such
a stream. Then, create a stream called fibs that contains the Fibonacci sequence. evenFibs and
oddFibs are similar but they only contain the even Fibonacci numbers and odd Fibonacci numbers,
respectively. The last stream is primes, which has the sequence of prime numbers.
Write a function called rev_zip_diff which will zip together two streams in reverse order (hint:
take a look at how the zip function works that we went over in class) and leverage an arbitrary
difference function to obtain the difference of every two elements. rev_zip_diff should have the
following type signature:
val zip
'a stream -> 'b stream -> ('b* 'a -> 'c) -> ('b * 'a * 'c) stream
You may wish to test rev_zip_diff by zipping together evenFibs and oddFibs and a function
that gets the integer difference, and then create a concrete list with the take function.
4 Generalized Printing
You are tasked with creating generalized list printing functions. Write a function called print GenList
which takes a list and a printing function f and applies the function to each element of the list
recursively. The function should have the following type signature:
val printGenList : 'a list -> ('a -> unit) -> unit
Create a function called printList that will pretty print an integer list. You will find the string
concatenation operator (^) and the string_of_int function useful. printList will take an integer
Transcribed Image Text:To start, write a function called even which returns true if the integer argument is even, false oth- erwise. Write a function called odd which returns true if the integer argument is odd, false otherwise. Now create five streams: squares, fibs, evenFibs, oddFibs, and primes. squares contains the sequence of perfect squares starting from 1. You may find the map function helpful to create such a stream. Then, create a stream called fibs that contains the Fibonacci sequence. evenFibs and oddFibs are similar but they only contain the even Fibonacci numbers and odd Fibonacci numbers, respectively. The last stream is primes, which has the sequence of prime numbers. Write a function called rev_zip_diff which will zip together two streams in reverse order (hint: take a look at how the zip function works that we went over in class) and leverage an arbitrary difference function to obtain the difference of every two elements. rev_zip_diff should have the following type signature: val zip 'a stream -> 'b stream -> ('b* 'a -> 'c) -> ('b * 'a * 'c) stream You may wish to test rev_zip_diff by zipping together evenFibs and oddFibs and a function that gets the integer difference, and then create a concrete list with the take function. 4 Generalized Printing You are tasked with creating generalized list printing functions. Write a function called print GenList which takes a list and a printing function f and applies the function to each element of the list recursively. The function should have the following type signature: val printGenList : 'a list -> ('a -> unit) -> unit Create a function called printList that will pretty print an integer list. You will find the string concatenation operator (^) and the string_of_int function useful. printList will take an integer
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education