Quick actions

cmd+k|ctrl+k

Navigation

Languages

Foldable and traversable

Snippet info

Language

Haskell

Visibility

public

Author

rightfold

Created

2018-04-13T18:29:27Z

Updated

2018-04-13T18:35:59Z

{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}

module Main
  ( main
  ) where

data Tree a = a :<<: [Tree a]
  deriving (Show, Functor, Foldable, Traversable)

main :: IO ()
main = do
  putStr "Example tree: "
  print example

  putStr "\nEach element transformed (fmap): "
  print (fmap negate example)

  putStr "\nAll elements summed (foldl): "
  print (foldl (+) 0 example)

  putStr "\nAll elements printed, then the result of that printed (traverse): "
  print =<< traverse print example
  where
    example :: Tree Int
    example = 1 :<<: [2 :<<: [], 3 :<<: [4 :<<: [], 5 :<<: []], 6 :<<: []]
INFO