Question

In: Computer Science

a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality...

a splitting function, split_by

Write a splitting function named split_by that takes three arguments

  1. an equality checking function that takes two values and returns a value of type bool,

  2. a list of values that are to be separated,

  3. and a list of separators values.

This function will split the second list into a list of lists. If the checking function indicates that an element of the first list (the second argument) is an element of the second list (the third argument) then that element indicates that the list should be split at that point. Note that this "splitting element" does not appear in any list in the output list of lists.

For example,

  • split_by (=) [1;2;3;4;5;6;7;8;9;10;11] [3;7] should evaluate to [ [1;2]; [4;5;6]; [8;9;10;11] ] and

  • split_by (=) [1;2;3;3;3;4;5;6;7;7;7;8;9;10;11] [3;7] should evaluate to [[1; 2]; []; []; [4; 5; 6]; []; []; [8; 9; 10; 11]].

    Note the empty lists. These are the list that occur between the 3's and 7's.

  • split_by (=) ["A"; "B"; "C"; "D"] ["E"] should evaluate to [["A"; "B"; "C"; "D"]]

Annotate your function with types.

Also add a comment explaining the behavior of your function and its type. Try to write this function so that the type is as general as possible.

OCaml code without using rec keyword in it.

Solutions

Expert Solution

Given question, we need to write split_by function in ocaml, taking three arguments, below is snipet code with detail implementation of accoriding to given points in question. Three arguments include first equality operator, second list and thirst splitting element, below is solution snippet with detailed explaination and tested split_by function with sample output given in question.

Solution snippet:

(* is_elem_by function returns true if list right fold matching with element_list *)
(* we will use this in split_by for checking seperator with x value *)
let is_elem_by (funcBool:'a -> 'b -> bool) (element_list:'b) (forlist:'a list): bool = 
        List.fold_right (fun x y -> if (funcBool x element_list) then true else y) forlist false


(* As stated in question, split_by function taking three arguments *)
let split_by (funcBool:'a -> 'b -> bool) (forcompletelist:'b list) (seperator:'a list): 'b list list =
  

        (* check if given x y elements match in function *)
        let funcBool x y =
        if (is_elem_by funcBool x seperator)
        (* for matching element with seperator *)
        then []::y
        (* return list else match tail *)
        else match y with
          | hd::tail -> (x::hd)::tail
          | [] -> [] in
          (* returns final list after evaluating matching element *)
        List.fold_right funcBool forcompletelist [[]]

Code screenshot if having problem with indentation in snippet code.

Output: test out split_by function:

Test for empty list:


Related Solutions

Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
Write code to define a function named mymath. The function has three arguments in the following...
Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer. The function will return a value as follows: 1. If the Boolean variable is True, the function returns the sum of the two integers. 2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Function named FunCount takes three arguments- C, an integer representing the count of elements in input...
Function named FunCount takes three arguments- C, an integer representing the count of elements in input list IP- input list of positive integers. Item- an integer value. Function FunCount returns an integer representing the count of all the elements of List that are equal to the given integer value Key. Example: Don’t take these values in program, take all inputs from user C = 9, IP= [1,1,4,2,2,3,4,1,2], Item = 2 function will return 3 IN C PROGRAMMING
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If  
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If   value1   and   value2   are   either   integers   or   strings   containing   only   digits,   cast   value1   and   value2   to   integers   and   compute   their   average.       If   their   average   is   greater   than   50,   return   the   string   “Above   50”   and   if   the   average   is   less   than   50,   return   the   string   “Below   50”.       If   the   average   is   equal   to   50,   return   the   string   “Equal   to   50”,   and   if   value1   or  ...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
(python) Write a function that involves two arguments, named changeTheCase(myFile, case),thattakes, as arguments, the name of...
(python) Write a function that involves two arguments, named changeTheCase(myFile, case),thattakes, as arguments, the name of a file, myFile, and the case, which will either be"upper"or"lower".If case is equal to "upper" the function will open the file, convert all characters on each line to upper case, write each line to a new file, named "upperCase.txt", and return the string "Converted file to upper case."If case is equal to "lower" the function will open the file, convert all characters on each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT