I am new to OCaml, and I am now trying to implement a function that returns a list containing all the elements in even position in the input list. For [1;2;3;5] returns [2;5] and for [1 3] returns [3]; and for ["i";"am";"new";"to";"ocaml"] returns ["am";"to"]. Hope someone can give me some suggestions to solve this problem.
相关问题
- Writing an interpreter in OCaml [closed]
- Relation between Function1 and Reader Monad
- scala passing function with underscore produces a
- Using Core.Std.List.fold_left without label
- See if key exists in a String Map
相关文章
- Is there something like the threading macro from C
- Learning F#: What books using other programming la
- Creating a list of functions using a loop in R
- ocaml %identity function
- When to use interfaces, and when to use higher ord
- Functors in Ocaml
- Java Lambda Referencing Enclosing Object: Replace
- Are 'currying' and 'composition' t
There's nothing particularly OCaml-like in this problem. Most likely your main task is to learn to solve problems recursively.
The way to solve a problem recursively is to break it into two cases:
a. The input is so small the answer is ridiculously obvious.
b. The answer isn't so small, but you already have a function that works for (strictly) smaller inputs.
To solve the problem, you need to decide how to determine whether the input size is ridiculously small (a very short list), and how to use another function that works for smaller lists to solve your problem with a larger list. Then your own function is that other function (when called recursively).
Well, you want to descend through your list getting elements two by two and forgetting the first one.
Then, you need to make sure of what happens when you get to the end of the list. But there can be two ways to get there, depending on the parity of
List.length l
.So, your final code would look like:
Note that this function is not tail-recursive, meaning it will put weight on your stack. This is not a problem over lists smaller than your stack, but may be problematic if you're going to handle big data. In that case, you may want to use a data structure more adapted to your problem.
Here is one solution:
You would call it like this:
I am iterating over the list:
checking if I am at an even index:
and if I am I will append it to the accumulator:
If I am not I won't:
Also note that for the first argument I pass in t not l. Which is the tail of the list.
At the end of the list I return the accumulator which should have all the even elements: