3 ms·
You could almost say that, if there was any notion of purity in javascript. You don't get referential transparency within javascript so talking about purity in
by papamonad 5y ago
You could almost say that, if there was any notion of purity in javascript. You don't get referential transparency within javascript so talking about purity in that case is really moot.
The main point is: Within haskell we have purity, if you have Int -> Int it is referentially transparent, it can't do anything but take inputs and produce outputs.
If we have an expression like
let x = y + z
y = 10 + z
z = 3
these are just expressions that are reduced to a type and a value.
IO () is a type, a higher kinded type that satisfies properties the same way a list does. This type has meaning and the meaning is some "Side effectful action producing something". we can manipulate, map and combine this type and it just represents some action to be performed, which is pure as it just represents the actions. The value is the actions to perform.
A haskell program is an expression that reduces to the type of an action IO ().
So while yes both the IO monad and the js can represent the same effects, it isn't equivalent when we are looking at something that was never pure to begin with and drawing the similarities of what they represent.
- goto11 5y agoOf course you can have purity in JavaScript. This function is referentially transparent and pure: (a, b) => a + b; While this function is not pure: (msg) => console.log(msg) However by your definition I guess the above function is actually also pure, since it can be passed around and manipulated without executing it? The cool thing about Haskell is that purity can be statically guaranteed by the type system and that unpure function are explicitly marked as such in their type signature.
- papamonad 5y agoThat function isn't referentially transparent as enforced by the language/compiler, doing no effects in a function isn't the same as having a referentially transparent function. Nothing is stopping you from logging/introducing side effects in (a,b) => a + b; you chose not to which doesn't make it referentially transparent. Being enforced or statically guaranteed is what makes haskell have purity and js not have it.