I need to create a program to check if a list increases then decreases, just like in the example below:
[1,2,3,4,5,6,4,3,2,1]
and it must be at least a one step increase or decrease.
Basically:
- there must be a single ascending sequence followed by a single descending sequence.
- the step in each transition must be at least one (no identical numbers side by side).
- the step can be more than one.
I thought about finding the biggest number in the list and then splitting the list into two lists, then checking if they are both sorted. How can it be done easier?
Alternatively :
That requires your implementation to have an
append/2
predicate defined, which is the case if you use swi for example. An adaptation to useappend/3
isn't hard to code though.If all numbers used are integers, consider using clpfd!
Based on
chain/2
, we can defineup_down_zs/3
like this:First, some cases we all expect to fail:
Now, let's run some satisfiable queries!
Here is how you can do it easier:
The first predicate checks whether the sequence is going up. When we get to the inflexion point, the second one is true. After that, we just have to check that it goes down until the end (last three).