Question: I need help to solve this problem in Ocaml Define the function everyNth : ('a list) -> int -> ('a list) that will take a
I need help to solve this problem in Ocaml
Define the function
everyNth : ('a list) -> int -> ('a list) that will take a list and a positive number i and produce a new list that has every element whose position in the given list is a multiple of i. Some example outputs for the function follow:
# everyNth [1;2;3;4] 2;; - : int list = [2; 4] # everyNth [1;2;3;4] 1;; - : int list = [1; 2; 3; 4] # everyNth [1;2;3;4;5;6] 3;; - : int list = [3; 6] # everyNth [1;2;3;4;5;6] 7;; - : int list = [] #
Use the function everyNth to define the function everyThird that takes the third element of each given list. Note that your solution must use everyNth, i.e. it is not okay to define everyThird from first principles. In case there is any doubt, the behavior expected from the function is of the following kind:
# everyThird [1; 2; 3; 4; 5];; - : int list = [3] # everyThird [1; 2; 3; 4; 5; 6];; - : int list = [3; 6] # everyThird [];; - : 'a list = [] #
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
