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 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...
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 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++
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT