Questions tagged [recursion-schemes]

Recursion schemes are reusable patterns for making recursive calls. They include catamorphisms and anamorphisms. This tag covers both the general concept, and the Haskell library of the same name.

Recursion schemes are reusable patterns for making recursive calls. They include catamorphisms and anamorphisms. This tag covers both the general concept, and the Haskell library of the same name which implements them using higher-order functions and type families.

cf. the recursion-schemes package.

See also

107 questions
4
votes
3 answers

Using recursion schemes in Haskell for solving change making problem

I'm trying to understand histomorphisms from this blog on recursion schemes. I'm facing a problem when I'm running the example to solve the change making problem as mentioned in the blog. Change making problem takes the denominations for a currency…
twitu
  • 553
  • 6
  • 12
4
votes
0 answers

Composing non-distributive monads in recursion-schemes

One of my favourite things about the recursion schemes in Haskell are the generalised morphisms (gcata etc.) that allow interleaving (co-)monadic computations with recursion, using a monad transformer library. For example, as described in this great…
Luciano
  • 2,388
  • 1
  • 22
  • 33
4
votes
1 answer

Express a futumorphism specialized to lists as an imperative loop

I've been trying to translate this recursive Haskell implementation of a futumorphism specialized to Lists futuL :: (a -> Maybe (b, ([b], Maybe a))) -> a -> [b] futuL f x = case f x of Nothing -> [] Just (y, (ys, mz)) -> y : (ys ++ fz) where…
user5536315
4
votes
1 answer

Recursion schemes with several types

Right now, I've got an AST for expression that's polymorphic over the type of recursion: data Expr a = Const Int | Add a a This has been incredibly useful by allowing me to use a type for plain recursion (Fix Expr) and another one when…
xal
  • 127
  • 1
  • 5
4
votes
2 answers

RamdaJS reduceBy() in Haskell using recursion-schemes

I have the following code using recursion-schemes library: {-# LANGUAGE LambdaCase #-} {-# LANGUAGE TypeFamilies #-} import Data.Functor.Foldable import Data.Maybe import qualified Data.Map as M reduceBy valueAlgebra keyFn = cata $ fooAlgebra…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
4
votes
1 answer

mapAccumR -like recursion scheme over Fix?

I'm playing with the functions from recursion-schemes, and struggling to figure out if it offers something that generalized mapAccumR. Something powerful enough to implement e.g.: f :: [Int] -> (Int,[Int]) f [] = (0,[]) f (x:xs) = let (sz,xs') = f…
jberryman
  • 16,334
  • 5
  • 42
  • 83
4
votes
3 answers

Is there something like cata but where you can match inner structure?

I have this language AST data ExprF r = Const Int | Var String | Lambda String r | EList [r] | Apply r r deriving ( Show, Eq, Ord, Functor, Foldable ) And I want to convert it to…
ais
  • 2,514
  • 2
  • 17
  • 24
3
votes
1 answer

How to implement an anamorphism such that it can be build upon any value of the the return type (rather than just the base case)

I've got anamorphism defined as follows: -- Fixed point of a Functor newtype Fix f = In (f (Fix f)) deriving instance (Eq (f (Fix f))) => Eq (Fix f) deriving instance (Ord (f (Fix f))) => Ord (Fix f) deriving instance (Show (f (Fix f))) => Show…
cocorudeboy
  • 119
  • 5
3
votes
2 answers

removing explicit recursion by replacing catamorphism

I have this AST data structure data AST = Integer Int | Let String AST AST | Plus AST AST | Minus AST AST | Times AST AST | Variable String | Boolean Bool | If AST AST AST |…
3
votes
1 answer

How is this hylo solution to the "coin-change" problem designed?

I came across a nice post on SO by @amalloy while looking for hylomorhism examples, that illustrate recursion scheme (RS) usage with useful discussion and full implementation: {-# LANGUAGE DeriveFunctor #-} import Control.Arrow ( (>>>), (<<<)…
3
votes
2 answers

How to fold data structures from non-tail recursive algorithms?

I have a variadic lifting function that allows for flat monadic chains without deeply nested function composition: const varArgs = f => { const go = args => Object.defineProperties( arg => go(args.concat(arg)), { "runVarArgs":…
user5536315
3
votes
1 answer

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

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 …
user5536315
3
votes
1 answer

Learning recursion schemes in Haskell by TicTacToe

Looking around, I noticed that recursion schemes are quite a general concept and I wanted to learn by experiencing it first hand. So I started implementing a minimax algorithm for TicTacToe. Here is a small snippet that sets the stage. Feel free to…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
3
votes
1 answer

Histomorphism a la Mendler

Using a histomorphism (histo) from recursion-schemes I can get the a list containing only the odd indexes from an initial list: import Data.Functor.Foldable odds :: [a] -> [a] odds = histo $ \case Nil -> [] Cons h (_…
Marius Catalin
  • 349
  • 1
  • 7
3
votes
1 answer

Forgetting Cofree annotations using a catamorphism

I have an AST that I'm annotating using Cofree: data ExprF a = Const Int | Add a a | Mul a a deriving (Show, Eq, Functor) I use type Expr = Fix ExprF to represent untagged ASTs, and type AnnExpr a = Cofree ExprF a to…