4 ms·
Yes and you can have your monads too, in JavaScript
by galaxyLogic 7y ago
Yes and you can have your monads too, in JavaScript
- Koshkin 7y agoBut see Greenspuns Tenth Rule of Programming (substitute Haskell for Common Lisp and JavaScript for Fortran).
- gridlockd 7y agoI do not believe that the rationale behind this rule applies to any dynamic/scripting language. It would apply to Haskell though, unless Haskell itself works well as a scripting host.
- crimsonalucard 7y agoYou can have monads in any language. If you have a functor you have a monad. If you have a type that wraps or encodes another type in a lossless or lossy way you have a functor. Therefore a functor exists in any language with compound types therefore every language that has compound types has monads. The difference is Haskell makes these concepts explicit.
- galaxyLogic 7y agoI would be interested in learning more about how does Haskell make monads "explicit"? Does Haskell have the keyword "monad"? Does Haskell have support for ensuring that monadic laws hold for any prospective monad?
- crimsonalucard 7y agoHaskell provides it as an "interface" in the "standard library" http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#t:Monad http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelud... You have to implement the methods but things like associativity and the existence of identity are not enforced. Additionally all IO functions in haskell are monads with a return type that is a functor called IO that can contain another type or nothing. You cannot print something to console without invoking a monad in haskell. The clever thing about the IO functor type is that it can only be instantiated by a true IO function that touches some external thing, there is no other way to instantiate this type. So for example the Instantiated type IO(int) cannot exist unless someone called a function that receives integers from a socket. And to extract the int from the functor you have to use the monadic bind operator.
- galaxyLogic 7y agoI see. Thanks. So the library-interface named "monad" includes the special machinery for programming monads. Libraries written in other languages could provide something similar, I assume.
- crimsonalucard 7y agoYeah. There's nothing special, it's purely a library which you can reimplement yourself in haskell or in other languages. The only thing special about it is the IO thing I mentioned which is an intrinsic part of the language. I know you mentioned javascript earlier, just know that while javascript CAN technically have monads, you kind of have to go out of your way to implement it. If you get some experience with a language that has an GADT system and really nail down the concept of a functor you'll see the full power of the monad. I generally would avoid using a monad with languages that don't have GADT systems.