Update: I can’t use any List.function stuff.
I’m new to OCaml and I’m learning this course in which I’m supposed to calculate a list of non decreasing values from a list of values.
So for e.g. I have a list [1; 2; 3; 1; 2; 7; 6]
So function mono that takes in a list returns the following:
# mono [1; 2; 3; 1; 2; 7; 6];;
- : int list = [1; 2; 3; 7]
I do the following:
let rec calculateCheck value lst = (
match lst with
[] -> true
| x :: xs -> (
if (value < x) then
false
else
calculateCheck value xs
)
);;
let rec reverse_list lst = (
match lst with
[] -> []
| x :: xs -> (
reverse_list xs @ [x]
)
);;
let shouldReverse = ref 1;;
let cancelReverse somelist lst = (
shouldReverse := 0;
reverse_list lst
);;
let rec mono lst = (
let somelist = ref lst in
if (!shouldReverse = 1) then
somelist := cancelReverse somelist lst
else
somelist := lst;
match !somelist with
[] -> []
| x :: xs -> (
if (calculateCheck x xs) then
[x] @ mono xs
else
[] @ mono xs
);
);;
Problem?
- This only works once because of shouldReverse.
- I cannot reverse the value;
mono listshould return non decreasing list.
Question?
- Any easy way to do this?
- Specifically how to get a subset of the list. For e.g. for [1; 2; 3; 5; 6], I want [1; 2; 3] as an output for 5 so that I can solve this issue recursively. The other thing, is you can have a list as [1; 2; 3; 5; 6; 5]:: so for the second 5, the output should be [1; 2; 3; 5; 6].
Any ideas?
Thanks
A good way to approach this kind of problem is to force yourself to
formulate what you’re looking for formally, in a mathematically
correct way. With some training, this will usually get you
a description that is close to the final program you will write.
We are trying to define a function
incr lithat contains thea strictly increasing subsequence of
li. As Jeffrey Scoffield asked,you may be looking for the
longest
such subsequence: this is an interesting and non-trivial algorithmic
problem that is well-studied, but given that you’re a beginner
I suppose your teacher is asking for something simpler. Here is my
suggestion of a simpler specification: you are looking for all the
elements that are greater than all the elements before them in the
list.
A good way to produce mathematical definitions that are easy to turn
into algorithms is reasoning by induction: define a property on
natural numbers
P(n)in terms of the predecessorP(n-1), or definea property on a given list in terms of this property on a list of one
less element. Consider you want to define
incr [x1; x2; x3; x4]. Youmay express it either in terms of
incr [x1; x2; x3]andx4, or interms of
x1andincr [x2; x3; x4].incr [x1;x2;x3;x4]isincr[x1;x2;x3], plusx4if it is biggerthan all the elements before it in the list, or, equivalently, the
biggest element of
incr[x1;x2;x3]incr [x1;x2;x3;x4]isincr[x2;x3;x4]where all the elementssmaller than
x1have been removed (they’re not bigger than allelements before them), and
x1addedThese two precise definitions can of course be generalized to lists of
any length, and they give two different ways to write
incr.