private static LabelLabeledTreePair <TState, T> LabelWithState <TState, T>(Tree <T> tree, Func <TState, TState> updateState, TState state) { if (tree is Leaf <T> ) { var leaf = (tree as Leaf <T>); return(LabelLabeledTreePair.Create(updateState(state), Leaf.Create(StateContentPair.Create(state, leaf.Content)))); } if (tree is Branch <T> ) { var branch = (tree as Branch <T>); var left = LabelWithState(branch.Left, updateState, state); // recursive call var right = LabelWithState(branch.Right, updateState, left.State); // threading return(LabelLabeledTreePair.Create(right.State, Branch.Create(left.LltpTree, right.LltpTree))); } throw new Exception("Lab/Label: impossible tree subtype"); }
public static StateMonad <TState, IEnumerable <StateContentPair <TState, TContent> > > CreateStateMonadFunctionally <TState, TContent>(IEnumerable <TContent> list, StateMonad <TState, TState> updateMonad) { var monad = list .Aggregate(StateMonad.Return <TState, IEnumerable <StateContentPair <TState, TContent> > >(new List <StateContentPair <TState, TContent> >()), (current, s) => current .Bind(x1 => updateMonad .Bind(x => StateMonad.Return <TState, IEnumerable <StateContentPair <TState, TContent> > >(new[] { StateContentPair.Create(x, s) })) .Bind(x2 => StateMonad.Return <TState, IEnumerable <StateContentPair <TState, TContent> > >(x1.Concat(x2))))); return(monad); }
private static StateMonad <TState, IEnumerable <StateContentPair <TState, TContent> > > CreateStateMonadRecursively <TState, TContent>(IReadOnlyCollection <TContent> list, StateMonad <TState, TState> updateMonad, StateMonad <TState, IEnumerable <StateContentPair <TState, TContent> > > result) { if (!list.Any()) { return(result); } return(CreateStateMonadRecursively(list.Skip(1).ToList(), updateMonad, result .Bind(res => updateMonad .Bind(state => StateMonad.Return <TState, IEnumerable <StateContentPair <TState, TContent> > >(new[] { StateContentPair.Create(state, list.First()) })) .Bind(nextItem => StateMonad.Return <TState, IEnumerable <StateContentPair <TState, TContent> > >(res.Concat(nextItem)))))); }
private static StateMonad <TState, Tree <StateContentPair <TState, T> > > CreateLabeledTree <TState, T>(Tree <T> tree, StateMonad <TState, TState> updateMonad) { if (tree is Leaf <T> ) { var leaf = (tree as Leaf <T>); return(updateMonad.Bind(n => StateMonad.Return <TState, Tree <StateContentPair <TState, T> > >(Leaf.Create(StateContentPair.Create(n, leaf.Content))))); } if (tree is Branch <T> ) { var branch = (tree as Branch <T>); // recursion return(CreateLabeledTree(branch.Left, updateMonad) .Bind(newleft => CreateLabeledTree(branch.Right, updateMonad) .Bind(newright => StateMonad.Return <TState, Tree <StateContentPair <TState, T> > >(Branch.Create(newleft, newright))))); } throw new Exception("MakeMonad/MLabel: impossible tree subtype"); }