(this function is nonsense, but there is a general pattern of, you do some I/O, probably a read, and then fmap the result to transform it, e.g. `fmap read getStrLn`)
-
-
Prikaži ovu nitHvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
do-notation for fmap is just do-notation, assuming the thing you want implements Monad: square :: Int -> IO Int square n = do print ("squaring " ++ show n) pure $ n * n
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
I like to use <$>
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
-
-
There is not, but fmap f x is the same as do y <- x pure (f y) if your Functor is also a Monad. Using do notation where you only need Functor is arguably overkill, but imo it’s also often clearer and rarely has drawbacks
-
(The drawback being that now your function no longer works with functors which aren’t monads)
Kraj razgovora
Novi razgovor -
-
-
print ("squaring " ++ show n) *> pure (n*n)
-
or >>. I forget what the laws are for *>
Kraj razgovora
Novi razgovor -
-
-
I'd usually write square n = (n*n) <$ putStrLn ...
Hvala. Twitter će to iskoristiti za poboljšanje vaše vremenske crte. PoništiPoništi
-
Čini se da učitavanje traje već neko vrijeme.
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.