Skip to content

Instantly share code, notes, and snippets.

@wnadurski
Created November 16, 2020 10:07
Show Gist options
  • Save wnadurski/a165b172e723fce35d92cf4c9b314542 to your computer and use it in GitHub Desktop.
Save wnadurski/a165b172e723fce35d92cf4c9b314542 to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Data.Functor.Mu (Mu(..))
import Effect (Effect)
import Effect.Console (log)
import Matryoshka (class Recursive, Algebra, cata)
data ListF a b
= NilF
| ConsF a b
derive instance listFFunctor :: Functor (ListF a)
type List a = Mu (ListF a)
concat :: forall a. Show a => Algebra (ListF a) String
concat NilF = ""
concat (ConsF a rest) = show a <> rest
concatList :: forall t a. Recursive t (ListF a) => Show a => t -> String
concatList = cata concat
nil :: forall a. List a
nil = In NilF
cons :: forall a. a -> List a -> List a
cons a as = In (ConsF a as)
concat2 :: forall a. Show a => a -> String -> String
concat2 a rest = show a <> rest
foldr :: forall a b. b -> (a -> b -> b) -> List a -> b
foldr initial f = cata f'
where
f' :: Algebra (ListF a) b
f' NilF = initial
f' (ConsF a rest) = f a rest
someList :: List Int
someList = 1 `cons` (3 `cons` (10 `cons` nil))
main :: Effect Unit
main = do
log $ concatList someList
log $ foldr "" concat2 someList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment