Question
Ocaml coding question, Note: no recursive functions, fold filter reduce etc only! a splitting function, split_by Write a splitting function named split_by that takes three
Note: no recursive functions, fold filter reduce etc only!
a splitting function, split_by
Write a splitting function named split_by that takes three arguments
an equality checking function that takes two values and returns a value of type bool,
a list of values that are to be separated,
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\"]]
let is_elem_by f x l = let f = List.filter (fun n -> f n x) l in match f with |[] -> false |_ -> true
let is_elem v lst = is_elem_by (=) v lst
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started