Created
February 26, 2021 21:31
-
-
Save cblp/109a35b3fd68bca444a5b15758ccf744 to your computer and use it in GitHub Desktop.
recursion-schemes example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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