9 ms·
Not sure I really follow. Every example you gave is just as intuitive in an imperative form as it is otherwise. Likely more. You want to teach someone how to
by taeric 13y ago
Not sure I really follow. Every example you gave is just as intuitive in an imperative form as it is otherwise. Likely more. You want to teach someone how to do a dance? Teach them the few basic steps, and then start showing them how to interlace said steps.
Hell, ever looked at the instructions for making your clothes? They speak in the language of seams and stitchings, yet are able to put together a whole clothing quite nicely. Even have room to adjust to things going wrong such as a thread or needle breaking during the process. (This is one area where exceptions have always seemed to me that they should be the continuation use case. Keep going until you hit an exception, fix the cause, then continue from where you left off.)
- dllthomas 13y agoI don't think you did follow. Nothing stops you from listing actions, in any language. In a language where actions are executed just by being listed unless particular magic constructs are used, you are more limited with what you can do with sequences of actions. Again, try to write something in logo that takes a list of arbitrary actions, and runs through the list executing each action twice before moving on to the next.
- taeric 13y agoBut isn't this already solved in an OOP world? So long as your "list of actions" is against a target (the "metaphor" in my original post), you can accomplish what you are referring to by manipulating that target. To go with my dance suggestion: function dance(dancer) { dancer.stepForward dancer.crossStepForward dancer.crossStepForward } Now, before the dancer is passed to this function, it is first passed to a "goOnPoint" function, such that now those directions are taken while on point. Or a "do all steps twice" function, for your direct example. The cooking metaphor is a little more directly applicable here. The cook is just configured to double (or half, etc) all ingredients used. Heck, the cook can even be configured such that substitute ingredients are known. Consider, it is trivial to use soy milk instead of regular milk for many "recipes". I actually like that example, because it doesn't always work. In a sense, it doesn't "type check." Some recipes have to basically be flagged as "milk substitutes not applicable." I fail to see how a monad really helps there.
- dllthomas 13y agoSure, you can go around and implement each special handling in every object that accepts actions. That potentially a lot of boiler plate, though. The functional approach is to view the actions as first class objects and operate on them before sequencing them together, whether that sequencing is done by an object in IO. > The cooking metaphor is a little more directly applicable here. The cook is just configured to double (or half, etc) all ingredients used. Heck, the cook can even be configured such that substitute ingredients are known. Consider, it is trivial to use soy milk instead of regular milk for many "recipes". > I actually like that example, because it doesn't always work. In a sense, it doesn't "type check." Some recipes have to basically be flagged as "milk substitutes not applicable." I fail to see how a monad really helps there. Again, the problem there is that you have to write your cook such that it can perform whatever transformations might be needed, rather than having the ability to apply arbitrary transformations. Regarding the type checking, I don't see how OO helps you there. In Haskell, monads (with type inference and typeclasses more generally) are absolutely a solution: instance Monad RecipeCanSubSoymilk where ... instance Monad RecipeNoSubSoymilk where ... instance Recipe RecipeCanSubSoymilk where ... instance Recipe RecipeNoSubSoymilk where ... noSoy :: RecipeNoSubSoymilk () milk :: Recipe r => Quantity -> r () flour :: Recipe r => Quantity -> r () subSoymilk :: Recipe r => RecipeCanSubSoymilk a -> r a This lets us say: subSoymilk (milk 10 >> flour 5) but provides a type error when we say: subSoymilk (milk 10 >> noSoy >> flour 5) and this will propagate through any depth of functions &c that we use to build our recipe. Though monads are overkill in this case: a list will do just fine, since we never need the result of a step to determine what to do next. But it's nice to have the flexibility when needed.
- taeric 13y agoSorry, when I said I fail to see how a monad helps here, I meant more in the "keeping the algorithm clear" sense. Simply put, there is a reason cookbooks do not resort to the idea of monads to convey directions. Thinking more on this, I am beginning to think a multiple phase "type checking" would make the most sense. Essentially, have the declarative ingredients list, the imperative commands, and optionally any declarative constraints. I strongly feel that the use of nominative subtyping is a very bad idea for the "constraints" of what goes into an algorithm leads to hardly readable stuff. I grant, though, that it may be more fruitful to train one's self to read these "hardly readable" things than it is to opine the lack of a better alternative.