Hi List,
Per my understanding, return x would make a new copy of the object. What if the returned object is mutable? Will this make a new (mutable) object? My concern is if I created a very large mutable object, does return mutable make a full copy of the original mutable data, or just copy a reference (pointer?)?
Thanks
baojun _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
To make my question more clearer, will test1/test2 have noticeable performance difference? -- mutable1.hs import qualified Data.Vector.Mutable as MV import Control.Monad import Control.Monad.Primitive a1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m () a1 v = do -- do something return () a2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m () a2 v = do -- do something else return () b1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a) b2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector (PrimState m) a) b1 v = do -- do something different return v b2 v = do -- do something else different return v test1 :: IO () test1 = do v1 <- MV.replicate 1000 0 a1 v1 a2 v1 return () test2 :: IO () test2 = MV.replicate 1000 0 >>= b1 >>= b2 >> return () -- I'd prefer this way cause it's more haskell. On Tue, Jun 17, 2014 at 5:50 PM, Baojun Wang <[hidden email]> wrote:
_______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Your understanding is not correct. The monad laws would be violated If return did make a new mutable object. http://www.haskell.org/haskellwiki/Monad_laws Side-effects typically have a `m ()` return type, so your "more Haskell" way is not idiomatic Haskell.
On Tue, Jun 17, 2014 at 7:53 PM, Baojun Wang <[hidden email]> wrote:
_______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Thanks a lot for the correction, I guess the right identity law would be violated, right? I was think about the type constructor stuff, next time should definitely think about the laws first.
On Tuesday, June 17, 2014, Bob Ippolito <[hidden email]> wrote:
_______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Baojun Wang
On Wed, Jun 18, 2014 at 2:53 AM, Baojun Wang <[hidden email]> wrote: To make my question more clearer, will test1/test2 have noticeable performance difference? No. The copy is by reference. -- Gregory Collins <[hidden email]>
_______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Free forum by Nabble | Edit this page |