|
| 1 | +% returns the longest increasing subsequence |
| 2 | +lis(Input, Output) :- |
| 3 | + % Aggregate all the increasing subsequences(In reverse order) |
| 4 | + % Compare the lengths and store the max length subsequence in Result |
| 5 | + aggregate(max(Len,IncSub), |
| 6 | + (one_is(Input, [], IncSub), length(IncSub, Len)), |
| 7 | + max(_, Result)), |
| 8 | + % Reverse and output the result |
| 9 | + reverse(Result, Output) |
| 10 | + . |
| 11 | + |
| 12 | + |
| 13 | +% find increasing subsequence |
| 14 | +% If input becomes empty, then return the string in the |
| 15 | +% current(accumulator) |
| 16 | +one_is([], Current, Current). |
| 17 | + |
| 18 | +one_is([H | T], Current, Final) :- |
| 19 | + % If the current(accumulator) is empty, push the head in accumulator |
| 20 | + % and recurse |
| 21 | + ( Current = [], one_is(T, [H], Final)); |
| 22 | + % If current is not empty, then push the head of input if |
| 23 | + % it is larger than the head of the accumulator and recurse |
| 24 | + ( Current = [H1 | _], H1 < H, one_is(T, [H | Current], Final)); |
| 25 | + one_is(T, Current, Final). |
0 commit comments