Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import Data.Array hiding (take)
- import Prelude
- import Control.Alternative (class Alternative, empty, (<|>))
- import Control.Apply (applySecond)
- import Control.Monad.List.Trans (ListT(..), cons, lift, nil, runListT, runListTRec, take)
- import Data.Foldable (class Foldable, foldr, oneOf, foldlDefault, foldrDefault)
- import Data.Generic.Rep (class Generic)
- import Data.Generic.Rep.Eq (genericEq)
- import Data.Generic.Rep.Show (genericShow)
- import Data.Lazy (defer)
- import Data.List.Lazy as LL
- import Effect (Effect)
- import Effect.Class (class MonadEffect, liftEffect)
- import Effect.Class.Console (log, logShow)
- import Data.Traversable
- import Data.Maybe
- data RoseTree a = RoseNode a (Array (RoseTree a))
- data Tree a = Empty | Node (Tree a) a (Tree a)
- derive instance genericTree :: Generic (Tree a) _
- derive instance functorTree :: Functor Tree
- instance showTree :: Show a => Show (Tree a) where
- show t = genericShow t
- instance applyTree :: Apply Tree where
- apply Empty _ = Empty
- apply _ Empty = Empty
- apply (Node lf f rf) (Node lv v rv) = Node (apply lf lv) (f v) (apply rf rv)
- foldMonoidTree :: forall a m. Monoid m => (a -> m) -> Tree a -> m
- foldMonoidTree _ Empty = mempty
- foldMonoidTree f (Node l v r) = foldMonoidTree f l <> f v <> foldMonoidTree f r
- -- Show (generic or manual)
- -- Functor
- -- Apply ?
- -- Applicative ?
- -- foldTree ?
- -- foldMonoidTree
- -- (a -> b) -> a -> b
- -- Functor f => (a -> b) -> f a -> f b
- -- Applicative f => f (a -> b) -> f a -> f b
- -- Monad f => (a -> f b) -> f a -> f b
- -- Traversable f => (a -> t b) -> m a -> t (m b)
- data List a = Nil | Cons a (List a)
- instance semigroupList :: Semigroup (List a) where
- append x y = foldr Cons y x
- instance functorList :: Functor List where
- map = go
- where
- go :: forall a b. (a -> b) -> List a -> List b
- go f Nil = Nil
- go f (Cons x xs) = Cons (f x) (go f xs)
- instance applyList :: Apply List where
- apply = go
- where
- go :: forall a b. List (a -> b) -> List a -> List b
- go Nil _ = Nil
- go _ Nil = Nil
- go (Cons f fs) xs = (f <$> xs) <> (go fs xs)
- instance applicativeList :: Applicative List where
- pure x = Cons x Nil
- instance foldableList :: Foldable List where
- foldl f = foldlDefault f
- foldr f = foldrDefault f
- foldMap = go
- where
- go :: forall a m. Monoid m => (a -> m) -> List a -> m
- go f Nil = mempty
- go f (Cons x xs) = f x <> go f xs
- instance bindList :: Bind List where
- bind l f = case l of
- Nil -> Nil
- Cons x xs -> f x <> bind xs f
- instance monadList :: Monad List
- instance traversableList :: Traversable List where
- sequence = sequenceDefault
- traverse f = foldr (\x xs -> Cons <$> (f x) <*> xs) (pure Nil)
- derive instance genericList :: Generic (List a) _
- instance showList :: Show a => Show (List a) where
- show l = genericShow l
- instance eqList :: Eq a => Eq (List a) where
- eq l = genericEq l
- list :: List Int
- list = Cons 10 (Cons 20 (Cons 30 Nil))
- list2 :: List (Int -> Int)
- list2 = Cons (_*10) (Cons (_*20) (Cons (_+30) Nil))
- main :: Effect Unit
- main = do
- for_ list $ \x -> do
- log $ show x <> "!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement