Post #2735432
2026-05-11 09:58 UTC
@byorgey@mathstodon.xyz For this you want the heterogeneous big brother of `lzw`:
```
lzw2 :: (a->b->c) -> a -> b -> [a] -> [b] -> [c]
lzw2 f u v (x:xs) (y:ys) = f x y : lzw2 f u v xs ys
lzw2 f u v xs [] = [ f x v | x [Int]
conjugate' = foldr incr []
where incr n ms = lzw2 ($) id 0 (replicate n succ) ms
```
Replies (1)
-
@jer_gib@functional.cafe 2026-05-11 09:59
@byorgey@mathstodon.xyz I'm in two minds about whether I prefer `lzw2` above or `lzw3` below: ``` lzw3 :: (a->b->c) -> (b->c) -> (a->c) -> [a] -> [b] -> [c] lzw3 f g h (x:xs) (y:ys) = f x y : lzw3 f g h xs ys lzw3 f g h xs [] = map h xs lzw3 f g h [] ys = map g ys ``` `lzw3` is more general (you can implement `lzw2` using it, and I think not vice versa), but at least in this case a bit clunkier to use: ``` conjugate' :: [Int] -> [Int] conjugate' = foldr incr [] where incr n ms = lzw3 ($) id ($0) (replicate n succ) ms ```