3 ms·
> What's the "pattern I've always used"? The pattern is to return a value and have some non-local return for errors. For example, having a return value in C++
by klodolph 8y ago
> What's the "pattern I've always used"?
The pattern is to return a value and have some non-local return for errors. For example, having a return value in C++ but throw an exception for errors. By "always used", I'm just asserting that most people are familiar with this pattern and use it frequently.
> Anyway, the problem is one of call/return in general, whether expressed in an FP language or not. It really wants to return something. With a real dataflow approach, that just goes away. And nothing is simpler than something.
This is not true for languages like Haskell. When you are writing monadic functions, it doesn't "want" to return something, instead, you are free to create your own data flow primitives as the monad's interface. This lets you change how control flow works in these monads while providing enough common structure that the novel control flow doesn't doesn't surprise people using the monad. Common ways to do this are by composing monad transformers, returning special value types, or using continuations.
That said, you get more flexibility to define data flow in Haskell if you combine the ordinary monadic parts with data flow libraries like pipes/conduit/machines. But you're still free to not return anything from a function (or technically a monadic action) in Haskell, as long as you're in a monad where returning nothing is a possibility.
- gowld 8y agoSince Haskell is non-strict, it has "dataflow", and deprecates "call/return" to some extent. (Parent poster might not have known that.) Function "calls" can be passed around but not executed unless needed. Monads like Maybe take advantage of this. Haskell functions always return something if they are called. Every Haskell program is "main :: IO ()", returning an IO action executed by the runtime, but in the Maybe monad that something can be Nothing.
- klodolph 8y agoWith monadic structure the non-strictness is not actually necessary. Haskell is a research language with a lot of features dumped into it, the fact that it’s non-strict has forced people to be more aware of whether their code is pure, and purity has motivated the research into things like monads. But you can do the same data flow programming even in an eager version of Haskell, it’s just that in eager languages programmers are more tempted to write impure functions in the first place.
- mpweiher 8y agoHmm...using the Maybe monad as counterexample to "call/return languages need a value to represent Nothing, because they need to return something" seems odd at best. "This is not a sentence."
- OJFord 8y ago> The pattern is to return a value and have some non-local return for errors. For example, having a return value in C++ but throw an exception for errors. By "always used", I'm just asserting that most people are familiar with this pattern and use it frequently. I think it's nonetheless a helpful model.