Skip to content

Instantly share code, notes, and snippets.

@cblp
Created February 26, 2021 21:31
Show Gist options
  • Save cblp/109a35b3fd68bca444a5b15758ccf744 to your computer and use it in GitHub Desktop.
Save cblp/109a35b3fd68bca444a5b15758ccf744 to your computer and use it in GitHub Desktop.
recursion-schemes example
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Mystudies where
import Data.Foldable (toList)
import Data.Functor.Foldable (fold)
import Data.Functor.Foldable.TH (makeBaseFunctor)
import Test.QuickCheck (quickCheckAll, (===))
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Foldable)
makeBaseFunctor ''Tree
tree :: Tree String
tree =
Node
(Node (Leaf "To") (Leaf "iterate"))
(Node
(Node (Leaf "is") (Leaf "human,"))
(Node (Leaf "to") (Node (Leaf "recurse") (Leaf "divine"))))
prop_folding_to_String_1 =
unwords (toList tree) === "To iterate is human, to recurse divine"
prop_folding_to_String_2 =
fold (\case LeafF s -> s; NodeF a b -> a ++ " " ++ b) tree
=== "To iterate is human, to recurse divine"
pure []
main = $quickCheckAll
-- $> main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment