I am new to OCaml, and I am now trying to implement a function that returns a list of elements of a given list x at indexes in list y.
For example, that function should perform the following computation: [5,6,7,8], [0, 3] => [5, 8]
I am not sure how to store temp variables in ML and don’t have a clear idea how it works. I do know how to find a element from a list given specified index, though.
Any idea will be appreciated, but I’d like to use recursive functions and avoid the List module.
No need for temporary variables, just use recursion!
It builds up the list by going through each of the indexes provided and then consing that onto the list returned by the next step.
Hopefully this is also optimised into a tail recursive form, but I’m not so sure about that. You may want to change it to be properly tail-recursive, but I’ll leave that up to you.