Exemplo n.º 1
0
        private static StateMonad <TState, Tree <Tuple <TState, TContents> > > MakeMonad(
            Tree <TContents> t,
            Func <StateMonad <TState, TState> > incrementer)
        {
            if (t is Leaf <TContents> )
            {
                var lf = (t as Leaf <TContents>);

                return(incrementer()
                       .Bind(n => StateMonad <TState, Tree <Tuple <TState, TContents> > >
                             .Return(new Leaf <Tuple <TState, TContents> >(Tuple.Create(n, lf.Contents)))));
            }

            if (t is Branch <TContents> )
            {
                var br       = (t as Branch <TContents>);
                var oldleft  = br.Left;
                var oldright = br.Right;

                return(MakeMonad(oldleft, incrementer)
                       .Bind(newleft => MakeMonad(oldright, incrementer)
                             .Bind(newright => StateMonad <TState, Tree <Tuple <TState, TContents> > >
                                   .Return(new Branch <Tuple <TState, TContents> >(newleft, newright)))));
            }

            throw new Exception("MakeMonad/MLabel: impossible Tree subtype");
        }
Exemplo n.º 2
0
        public static StateMonad <TState, TContentsB> Bind <TContentsB>(
            StateMonad <TState, TContents> inputMonad,
            Func <TContents, StateMonad <TState, TContentsB> > inputMaker)
        {
            return(new StateMonad <TState, TContentsB>(state0 =>
            {
                var lcp1 = inputMonad.StateMaker(state0);
                var state1 = lcp1.First;
                var contents1 = lcp1.Second;

                return inputMaker(contents1).StateMaker(state1);
            }));
        }