Monads in Haskell
Learning Monads from this brilliant series of explanations.
I enjoy functional programming and cookies.
The following are two simple procedures for ‘wrapped’ expressions:
(defn- wrapped? [_expression] " Detects if an expression is wrapped up in layers. 'y is not wrapped '(x) is wrapped on 'x '((((x + y)))) is wrapped on '(x + y) " (and (coll? _expression) (= 1 (count _expression)))) (defn- unwrap [_expression] " Iteratively unwraps an expression until it is no-longer wrapped. 'y --> 'y '((x)) --> 'x '(((((x * y))))) --> '(x * y) " (if (wrapped? _expression) (recur (first _expression)) _expression))
I authored these to help with my dissertation’s “tokenised infix expression” parser. They are used to eliminate singly-nested expressions.
Sub-expressions are now matched and swapped out if a reduction is made.
Conditionals evaluate with bindings correctly.
Executor forms also evaluate with bindings correctly.
I have a few abstract ideas about ensuring termination, but nothing properly explored yet.
As well as cycle capturing for termination, I also have to build an infix expression parser. For which, I am thinking about implementing a modified version of the shunting-yard algorithm. We’ll see how that goes.
I have a meeting with my project supervisor at 11am today. I’m wondering what he’ll say if I tell him I started from scratch two days ago and have already completed the core functionality. :D
Just have to write the recursive rule matcher and infix parser until my core functionality is complete!
Then I’ll go on to extend the reduction system to also destruct non list structures, so I can define rules as such:
(swap [a b]) = [b, a]
Or a more advanced example, defining a complete map function purely using rewriting rules.
(map x ) =  (map x [f]) = [(x f)] (map x [f & rest]) = (conj (map x rest) (x f))
Which will be pretty awesome.
So far I have macros for building a rewriting system, defining a named rewriting system, rules processing, variable & constant definitions, and parser definition - without an infix parsing implementation.