catamorphism

Can one express catamorphism through Data.Function.fix?

吃可爱长大的小学妹 提交于 2020-02-04 05:33:29
问题 I have this lovely fixana function here that performs about 5 times faster than her sister ana . (i have a criterion report to back me on this) ana alg = Fix . fmap (ana alg) . alg fixana alg = fix $ \f -> Fix . fmap f . alg Can I express their cousin cata in the same fashion? cata f = f . fmap (cata f) . unFix It seems to me that I cannot, but I have been humbled by my S.O. fellows quite a few times in the past. 回答1: Actually, this has nothing to do with catamorphisms. Whenever a function is

What are practical examples of the higher-order functions foldl and foldr?

心不动则不痛 提交于 2020-01-25 03:43:05
问题 The typical academic example is to sum a list. Are there real world examples of the use of fold that will shed light on its utility ? 回答1: fold is perhaps the most fundamental operation on sequences. Asking for its utility is like asking for the utility of a for loop in an imperative language. Given a list (or array, or tree, or ..), a starting value, and a function, the fold operator reduces the list to a single result. It is also the natural catamorphism (destructor) for lists. Any

How would I implement this fold function?

社会主义新天地 提交于 2019-12-23 15:25:23
问题 Given are the two datatypes Color and Plant. data Color = Red | Pink | White | Blue | Purple | Green | Yellow deriving (Show, Eq) data Plant = Leaf | Blossom Color | Stalk Plant Plant deriving (Show, Eq) Now I'm supposed to implement a function fold_plant of following type: (x -> x -> x) -> (Color -> x) -> x -> Plant -> x The way I understand a fold function is that it takes a list and for each iteration it removes the first element from the list and does something with that element.

What's the type of a catamorphism (fold) for non-regular recursive types?

╄→гoц情女王★ 提交于 2019-12-20 10:10:48
问题 Many catamorphisms seem to be simple enough, mostly replacing each data constructor with a custom function, e.g. data Bool = False | True foldBool :: r -- False constructor -> r -- True constructor -> Bool -> r data Maybe a = Nothing | Just a foldMaybe :: b -- Nothing constructor -> (a -> b) -- Just constructor -> Maybe a -> b data List a = Empty | Cons a (List a) foldList :: b -- Empty constructor -> (a -> b -> b) -- Cons constructor -> List a -> b However, what's not clear to me is what

How to work with AST with Cofree annotation?

拈花ヽ惹草 提交于 2019-12-17 23:15:46
问题 I have this simple Expr AST and I can easily convert it to String . import Prelude hiding (Foldable) import qualified Prelude import Data.Foldable as F import Data.Functor.Foldable import Data.Monoid import Control.Comonad.Cofree data ExprF r = Const Int | Add r r deriving ( Show, Eq, Ord, Functor, Prelude.Foldable ) type Expr = Fix ExprF testExpr = Fix $ Add (Fix (Const 1)) (Fix (Const 2)) convertToString :: Expr -> String convertToString = cata $ \case e@(Const x) -> show x e@(Add x y) ->

How to define the fibonacci sequence using a fold for natural numbers?

大兔子大兔子 提交于 2019-12-12 19:14:28
问题 I am currently learning folds in the sense of structural recursion/catamorphisms. I implemented power and factorial using a fold for natural numbers. Please note that I barely know Haskell, so the code is probably awkward: foldNat zero succ = go where go n = if (n <= 0) then zero else succ (go (n - 1)) pow n = foldNat 1 (n*) fact n = foldNat 1 (n*) n Next I wanted to adapt the fibonacci sequence: fib n = go n (0,1) where go !n (!a, !b) | n==0 = a | otherwise = go (n-1) (b, a+b) With fib I

Does each type have a unique catamorphism?

喜你入骨 提交于 2019-12-12 08:25:25
问题 Recently I've finally started to feel like I understand catamorphisms. I wrote some about them in a recent answer, but briefly I would say a catamorphism for a type abstracts over the process of recursively traversing a value of that type, with the pattern matches on that type reified into one function for each constructor the type has. While I would welcome any corrections on this point or on the longer version in the answer of mine linked above, I think I have this more or less down and

Does each type have a unique catamorphism?

喜夏-厌秋 提交于 2019-12-04 03:09:21
Recently I've finally started to feel like I understand catamorphisms. I wrote some about them in a recent answer , but briefly I would say a catamorphism for a type abstracts over the process of recursively traversing a value of that type, with the pattern matches on that type reified into one function for each constructor the type has. While I would welcome any corrections on this point or on the longer version in the answer of mine linked above, I think I have this more or less down and that is not the subject of this question, just some background. Once I realized that the functions you

What's the type of a catamorphism (fold) for non-regular recursive types?

五迷三道 提交于 2019-12-02 21:44:36
Many catamorphisms seem to be simple enough, mostly replacing each data constructor with a custom function, e.g. data Bool = False | True foldBool :: r -- False constructor -> r -- True constructor -> Bool -> r data Maybe a = Nothing | Just a foldMaybe :: b -- Nothing constructor -> (a -> b) -- Just constructor -> Maybe a -> b data List a = Empty | Cons a (List a) foldList :: b -- Empty constructor -> (a -> b -> b) -- Cons constructor -> List a -> b However, what's not clear to me is what happens if the same type constructor is used, but with a different type argument. E.g. instead of passing

What's the relation of fold on Option, Either etc and fold on Traversable?

对着背影说爱祢 提交于 2019-11-30 08:57:48
Scalaz provides a method named fold for various ADTs such as Boolean , Option[_] , Validation[_, _] , Either[_, _] etc. This method basically takes functions corresponding to all possible cases for that given ADT. In other words, a pattern match shown below: x match { case Case1(a, b, c) => f(a, b, c) case Case2(a, b) => g(a, b) . . case CaseN => z } is equivalent to: x.fold(f, g, ..., z) Some examples: scala> (9 == 8).fold("foo", "bar") res0: java.lang.String = bar scala> 5.some.fold(2 *, 2) res1: Int = 10 scala> 5.left[String].fold(2 +, "[" +) res2: Any = 7 scala> 5.fail[String].fold(2 +, "[