Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or non-negative numbers and a list of absolute values of the original list elements. Ensure that this...

1 answer below »

7

Ocaml Question

Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or
non-negative numbers and a list of absolute values of the original list elements. Ensure that this list is
in the same order.
To report multiple outputs, use a record. We'll define this particular output record as run_output :
type run_output =
{
length : int;
entries : int list;
}
Template:
let maxrun (l:int list) : run_output =
(** YOUR CODE HERE **)
Sample Unit Test
let out = moveNeg [8; -5; 3; 0; 10; -4] in
if out.length = 3
&& out.entries = [8; 5; 3; 0; 10; 4]
then print_string
Extracted text: Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or non-negative numbers and a list of absolute values of the original list elements. Ensure that this list is in the same order. To report multiple outputs, use a record. We'll define this particular output record as run_output : type run_output = { length : int; entries : int list; } Template: let maxrun (l:int list) : run_output = (** YOUR CODE HERE **) Sample Unit Test let out = moveNeg [8; -5; 3; 0; 10; -4] in if out.length = 3 && out.entries = [8; 5; 3; 0; 10; 4] then print_string "YAY" else raise (Failure "00PS");
Answered 122 days After Jun 04, 2022

Solution

Ajay answered on Oct 04 2022
54 Votes
Codes
un_output = {
   l = maxrun,
   list = abs_list
}
function maxrun(list)
   max = -1
   count = 1
   cu
ent = list[1]
   for i = 2,#list do
       element = list[i]
       if element >= 0 and cu
ent >= 0 then
           count = count + 1
       elseif element < 0 and cu
ent < 0 then
           count = count + 1
       else
           count = 1
       end
       if count > max then
           max = count
       end
       cu
ent = element
   end
   return {
       l = max,
       list = list
   }
end
function abs(list)
   for i = 1,#list do
       list[i] = math.abs(list[i])
   end
   return list
end
abs_list = abs([-6, 2, -3, -4, 9, -8,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here