Post #2735414
2026-05-11 14:01 UTC
Replies (2)
-
@jer_gib@functional.cafe 2026-05-11 14:01
@byorgey@mathstodon.xyz @oantolin@mathstodon.xyz @das_g@chaos.social In fact, this recursion is a `concat` after an unfold. So it's also a list futumorphism: ``` futu :: (b -> Maybe ([a],b)) -> b -> [a] futu g z = case g z of Nothing -> [] Just (ys, z') -> ys ++ futu g z' ``` (Not stated is the requirement that the generated chunk `ys` should be nonempty, in order to guarantee progress. Alternatively one can make the body return `Maybe (a,[a],b)`, enforcing the requirement structurally.) Then we have: ``` conjugate :: [Int] -> [Int] conjugate = futu strip where strip [] = Nothing strip ns = Just (replicate m (length ns), takeWhile (>0) [ n - m | n <- ns ]) where m = minimum ns ```
-
@oantolin@mathstodon.xyz 2026-05-11 14:59
@jer_gib@functional.cafe Does this version really have complexity O(length ns + maximum ns)? The minimum of ns is the last element which takes linear time for linked lists. So I think this is (length ns) * maximum ns, but you could make it run in the desired time by using arrays instead of lists. Am I missing something? @byorgey@mathstodon.xyz @das_g@chaos.social