higher-order-functions

Lifting a higher order function in Haskell

醉酒当歌 提交于 2019-11-26 08:24:02
问题 I\'m trying to construct a function of type: liftSumthing :: ((a -> m b) -> m b) -> (a -> t m b) -> t m b where t is a monad transformer. Specifically, I\'m interested in doing this: liftSumthingIO :: MonadIO m => ((a -> IO b) -> IO b) -> (a -> m b) -> m b I fiddled with some Haskell wizardry libs and but to no avail. How do I get it right, or maybe there is a ready solution somewhere which I did not find? 回答1: This can't be done generically over all MonadIO instances because of the IO type

What are paramorphisms?

爷,独闯天下 提交于 2019-11-26 04:59:41
问题 Reading through this classic paper, I\'m stuck on paramorphisms. Unfortunately the section is quite thin, and the Wikipedia page doesn\'t say anything. My Haskell translation is: para :: (a -> [a] -> b -> b) -> b -> [a] -> b para f base = h where h [] = base h (x:xs) = f x xs (h xs) But I don\'t grok that -- I don\'t have any intuition for the type signature or the desired result. What\'s a paramorphism, and what are some useful examples in action? Yes, I\'ve seen these questions, but they

Transposing multidimensional arrays in PHP

独自空忆成欢 提交于 2019-11-25 21:50:40
问题 How would you flip 90 degrees (transpose) a multidimensional array in PHP? For example: // Start with this array $foo = array( \'a\' => array( 1 => \'a1\', 2 => \'a2\', 3 => \'a3\' ), \'b\' => array( 1 => \'b1\', 2 => \'b2\', 3 => \'b3\' ), \'c\' => array( 1 => \'c1\', 2 => \'c2\', 3 => \'c3\' ) ); $bar = flipDiagonally($foo); // Mystery function var_dump($bar[2]); // Desired output: array(3) { [\"a\"]=> string(2) \"a2\" [\"b\"]=> string(2) \"b2\" [\"c\"]=> string(2) \"c2\" } How would you