例如,来自std :: deque :: operator = in C++ Reference:
(1)Copy Assignment (const std :: deque&other)
用其他内容的副本替换内容.
如果std :: allocator_traits :: propagate_on_container_copy_assignment()为true,则目标分配器将替换为源分配器的副本.如果目标和源分配器不比较相等,则使用目标(*this)分配器来释放内存,然后在复制元素之前使用其他分配器来分配它.
如果this->get_allocator() == other.get_allocator(),我可以简单地破坏和释放this,如果需要'元素,或分配,如果需要构建元素,或复制分配的元素other来*this如果需要的话.
但如果不是呢?上面的引用是否意味着我不能复制 - 分配元素,所以我必须首先销毁和释放所有元素,使用this->get_allocator(),然后分配和构造元素,使用other.get_allocator()?
但如果是这种情况,我为什么要other.get_allocator()用于分配呢?
以后不会导致一些运行时错误,因为this不会正确释放内存吗?
(2)移动作业 (std :: deque && other)
使用移动语义替换其他内容(即其他数据从其他数据移动到此容器中).其他是后来处于有效但未指定的状态.如果std :: allocator_traits :: propagate_on_container_move_assignment()为true,则目标分配器将替换为源分配器的副本.如果它为假并且源和目标分配器不比较相等,则目标不能获取源存储器的所有权,并且必须单独移动 - 分配每个元素,根据需要使用其自己的分配器分配额外的存储器.在任何情况下,*this中最初存在的所有元素要么被销毁,要么被元素移动赋值替换.
如果this->get_allocator() == other.get_allocator(),这是一项简单的任务.
但如果没有,则上面会出现同样的问题,除非在这种情况下使用移动分配.
在这两种情况下,我还有一个问题.
如果元素既不能复制分配也不能移动分配,是否可以销毁它并从其他元素构建?如果是,我应该使用谁的分配器?
由于我对 N 级类型不熟悉,所以 的类型签名gfoldl对我来说很麻烦:
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> a
-> c a
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一函数分别是\xs y -> ($y) <$> xs和pure。
其他函数如gunfold和 也gmapT有类似的问题。那么它们的重要用途有哪些值得注意的例子呢?
我在Ubuntu上。当我ghci在终端上运行并执行以下操作时:
Prelude Control.Monad System.IO> forever $ getChar >>= print
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
a'a'
b'b'
C'C'
%'%'
\'\\'
1'1'
''\''
"'"'
^X'\CAN'
^?'\DEL'
^CInterrupted.
Run Code Online (Sandbox Code Playgroud)
也就是说,我在键盘上输入的字符将被刷新到输出中。我怎样才能避免这种情况,只能print以作家的身份出现?
实例MonadPlus IO是唯一的,因为mzero抛出:
Prelude Control.Monad> mzero
*** Exception: user error (mzero)
Run Code Online (Sandbox Code Playgroud)
因此,相应地,这也MonadPlus IO 意味着它也适用于错误。
mzero 如果其他动作不抛出,则显然用作标识元素:
Prelude Control.Monad> mzero `mplus` return 0
0
Prelude Control.Monad> return 0 `mplus` mzero
0
Run Code Online (Sandbox Code Playgroud)
但是当两个动作都抛出时不是这样:
Prelude Control.Monad> fail "Hello, world!" `mplus` mzero
*** Exception: user error (mzero)
Prelude Control.Monad> mzero `mplus` fail "Hello, world!"
*** Exception: user error (Hello, world!)
Run Code Online (Sandbox Code Playgroud)
所以MonadPlus IO不是monoid。
如果MonadPlus在用户意图出错时违反法律,那么它的实际意图是什么?
作为一名前C ++程序员,Haskell线程的行为令人困惑。请参考以下Haskell代码段:
import Control.Concurrent
import Control.Concurrent.MVar
import Data.Functor.Compose
import System.Random
randomTill0 :: MVar () -> IO () -- Roll a die until 0 comes
randomTill0 mV = do
x <- randomRIO (0,65535) :: IO Int
if 0 == x
then putMVar mV ()
else randomTill0 mV
main :: IO ()
main = do
n <- getNumCapabilities
mV <- newEmptyMVar
sequence (replicate n (forkIO (randomTill0 mV)))
readMVar mV
putStrLn "Excution complete."
Run Code Online (Sandbox Code Playgroud)
据我所知,Haskell的forkIO大致相当于C ++的std::async。在C ++中,我存储一个std::future由返回的std::async …
我试图实现一个表示无限动作链的 monad 转换器,如下所示:
import Control.Arrow
import Control.Monad
import Data.Functor.Classes
import Data.Functor.Identity
import Data.Monoid
import Data.Semigroup
import Text.Read
import qualified Control.Monad.Trans.Class as T
newtype WhileT m a = WhileT {uncons :: m (a, WhileT m a)}
instance T.MonadTrans WhileT where
lift m = WhileT $ do
x <- m
pure (x, T.lift m)
headW :: Functor m => WhileT m a -> m a
headW (WhileT m) = fmap fst m
tailW :: Functor m => WhileT m a -> m …Run Code Online (Sandbox Code Playgroud) 大家都知道State是一个 monad:
import Control.Monad
newtype State s a = State {runState :: s -> (a, s)}
instance Functor (State s) where
fmap = liftM
instance Applicative (State s) where
pure x = State (\s -> (x, s))
(<*>) = ap
instance Monad (State s) where
State f >>= k = State $ \s -> let
(x, s2) = f s
State g = k x
in g s2
Run Code Online (Sandbox Code Playgroud)
但这也是箭头吗?这是我的实施尝试instance Arrow State:
import Control.Arrow
import Control.Category
instance …Run Code Online (Sandbox Code Playgroud) 标题是问题.
我仍然不太了解动态分配存储的行为.
#include <utility>
int main() {
// Case 1:
volatile char *c1 = new char; // I'm gonna make these volatile, so the compiler won't change the code.
volatile char *d1 = c1;
delete d1;
// Case 2:
volatile char *c2 = new char[4];
volatile char *d2 = c2;
delete []d2;
// Case 3:
volatile char *c3 = new char;
volatile char *d3 = std::move(c3);
delete d3;
// Case 4:
volatile char *c4 = new char[4];
delete c4++;
delete …Run Code Online (Sandbox Code Playgroud) 当我们有 时,为什么不IO实例化(严格)单子,如 中所提供的?我认为我们应该是代表现实本身的神奇类型。StateRealWorldControl.Monad.STRealWorld
我的意思是,回想一下 monad 的“run”函数State:
runState :: (s -> (a, s)) -> s -> a
Run Code Online (Sandbox Code Playgroud)
将其实例化为RealWorld,我们得到:
runIO :: (RealWorld -> (a, RealWorld)) -> RealWorld -> a
Run Code Online (Sandbox Code Playgroud)
RealWorld 由于无论如何我们都无法构造 的值,因此这不应该充当像 那样的后门unsafePerformIO。
原因是因为这种解释将启用 monad 转换器IOT,定义为StateT RealWorld?
所以,当我编译下面编辑的代码时:
instance (Eq a) => PartOrd a where
[] == [] = True
(x:xs) == (y:ys) = x==y && xs==ys
_ == _ = False
xs /= ys = not (xs == ys)
Run Code Online (Sandbox Code Playgroud)
我明白了:
`==' is not a (visible) method of class 'PartOrd'
`/=' is not a (visible) method of class 'PartOrd'
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
我已经看过如何在Haskell中正确实例化类?有些澄清,但即使我无法解决它.
另外,当我使用=~for ==和/~for 等定制运算符时,它是否相同/=,因为我得到了同样的错误?
编辑:根据要求:
class Eq a => PartOrd a where …Run Code Online (Sandbox Code Playgroud) 课堂上的traverse和sequenceA功能Traversable必须满足以下"自然性"规律:
t . traverse f == traverse (t . f)
t . sequenceA == sequenceA . fmap t
Run Code Online (Sandbox Code Playgroud)
对于每一次"应用转型" t.但它是什么?
它似乎不适instance Traversable []用于t = tail:
Prelude> tail . sequenceA $ [[1],[2,3]]
[[1,3]]
Prelude> sequenceA . fmap tail $ [[1],[2,3]]
[]
Run Code Online (Sandbox Code Playgroud)
也不是t = join (++)(重复两次列表):
Prelude Control.Monad> join (++) . sequenceA $ [[1],[2,3]]
[[1,2],[1,3],[1,2],[1,3]]
Prelude Control.Monad> sequenceA . fmap (join (++)) $ [[1],[2,3]]
[[1,2],[1,3],[1,2],[1,3],[1,2],[1,3],[1,2],[1,3]]
Run Code Online (Sandbox Code Playgroud)
因此,对于什么t他们满意吗?
当 a 和 b 都为真或它们都为假时,我试图得到真。有人能告诉我我做错了什么吗?
imply :: (a -> Bool, b -> Bool) -> Bool
a = False
b = True
imply (a, a) = True
imply (b, b) = True
imply _ = False
Run Code Online (Sandbox Code Playgroud) haskell ×10
c++ ×3
io ×2
monads ×2
state ×2
allocator ×1
applicative ×1
arrows ×1
assign ×1
c++11 ×1
equality ×1
flush ×1
input ×1
instance ×1
memory-leaks ×1
monoids ×1
output ×1
rank-n-types ×1
throw ×1
traversable ×1
typeclass ×1
while-loop ×1