Skip to content

Instantly share code, notes, and snippets.

@gallais
Created December 21, 2015 14:57
Show Gist options
  • Save gallais/0ee7d3e2b60e3bcedcc4 to your computer and use it in GitHub Desktop.
Save gallais/0ee7d3e2b60e3bcedcc4 to your computer and use it in GitHub Desktop.
Unfold function
{-# LANGUAGE ScopedTypeVariables #-}
module UnfoldTree where
data Tree v e = Node v [(e,Tree v e)]
unfold :: forall s v e. (s -> (v, [(e,s)])) -> s -> Tree v e
unfold next seed = uncurry Node $ fmap rec $ next seed where
rec :: [(e,s)] -> [(e, Tree v e)]
rec = fmap $ fmap (unfold next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment