Last active
September 11, 2019 12:16
-
-
Save prednaz/b8e003b25967cd8a0543668475e11732 to your computer and use it in GitHub Desktop.
list of ascending sub lists, version 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Tuple (swap) | |
ascending_sub_list :: Ord e => [e] -> [[e]] | |
ascending_sub_list [] = [] | |
ascending_sub_list list = ascending_sub_list_first_result : ascending_sub_list rest | |
where (ascending_sub_list_first_result, rest) = ascending_sub_list_first list | |
-- first ascending sub list and the rest | |
ascending_sub_list_first :: Ord e => [e] -> ([e], [e]) | |
ascending_sub_list_first [] = ([], []) | |
ascending_sub_list_first (list_head:list_tail) = | |
swap_map (list_head :) $ ascending_sub_list_continuation list_head list_tail | |
where | |
ascending_sub_list_continuation :: Ord e => e -> [e] -> ([e], [e]) | |
ascending_sub_list_continuation previous continuation_potential | |
| continuation_potential_head : continuation_potential_tail <- continuation_potential, | |
previous <= continuation_potential_head | |
= swap_map (continuation_potential_head :) | |
$ ascending_sub_list_continuation continuation_potential_head continuation_potential_tail | |
| otherwise = ([], continuation_potential) | |
swap_map f = swap . fmap f . swap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment