> Is this really so different from IO everywhere in Haskell?
It's a starting point. You'll have to somehow chain (e.g. you want to print "def" after "abc", how would you do that?). You'll also need to put a value into your IO, e.g. if you are inside that IO, all the computations will need to produce IO. If you solve those things, you'll have a Monad.
But, wait! You're in JS, right? You have IO Monads there, perhaps only don't know yet. They are the JS Promises! Look this:
// assuming that you have a sync input function, that receives
a string from stdin and returns it
let input_str = new Promise((resolve, reject) => resolve(input());
let input_int = input_str.then(s => parseInt(s));
let square = input_int.then(n => n * n);
square.then(sq => console.log(sq));
If you want to proceed with your poor man's IO, you'll need to implement your "then". Going further, the equivalent in Haskell (not the best code, though) to that is:
Ok, but Haskell has a syntax for cleaning that, called do notation:
main = do
input_str <- getLine
let input_int = read input_str :: Int
let square = input_int * input_int
putStrLn (show square)
And JS? JS has also its syntax, using await. Just like Haskell that it only works inside the do notation, in JS it will only work inside async functions:
let input_str = await new Promise((resolve, reject) => resolve(input()));
It's a starting point. You'll have to somehow chain (e.g. you want to print "def" after "abc", how would you do that?). You'll also need to put a value into your IO, e.g. if you are inside that IO, all the computations will need to produce IO. If you solve those things, you'll have a Monad.
But, wait! You're in JS, right? You have IO Monads there, perhaps only don't know yet. They are the JS Promises! Look this:
// assuming that you have a sync input function, that receives a string from stdin and returns it
let input_str = new Promise((resolve, reject) => resolve(input());
let input_int = input_str.then(s => parseInt(s));
let square = input_int.then(n => n * n);
square.then(sq => console.log(sq));
If you want to proceed with your poor man's IO, you'll need to implement your "then". Going further, the equivalent in Haskell (not the best code, though) to that is:
main = result
Ok, but Haskell has a syntax for cleaning that, called do notation:main = do
And JS? JS has also its syntax, using await. Just like Haskell that it only works inside the do notation, in JS it will only work inside async functions:let input_str = await new Promise((resolve, reject) => resolve(input()));
let input_int = parseInt(input_str);
let square = input_int * input_int;
console.log(square);