小编zli*_*zli的帖子

类型构造器currying?(试图创建一个接受一个类型的数据构造函数,而创建另一个接受两种类型的数据构造函数)

我刚刚开始学习Haskell,并且尝试通过制作纸牌游戏将其付诸实践。我正在尝试创建一个“手”类型,该类型表示固定大小的纸牌矢量(使用大小矢量,如下所述:https : //www.schoolofhaskell.com/user/konn/prove-your-haskell-for-great -安全性/ haskell中的依存类型

我对此进行了几次尝试,但是都没有成功:

{-# LANGUAGE GADTs, KindSignatures, DataKinds, PolyKinds #-}
{-# LANGUAGE TypeFamilies, ScopedTypeVariables #-}

import Data.Type.Natural
import Data.Vector.Sized (Vector)
import Card -- | I have the Card type defined in another file

data Hand (n :: Nat) where
    Hand :: SNat n -> Vector Card n

{- fails with:
* Data constructor 'Hand' returns type 'Vector Card n'
    instead of an instance of its parent type 'Hand n'
* In the definition of data …
Run Code Online (Sandbox Code Playgroud)

haskell types currying dependent-type type-constructor

3
推荐指数
1
解决办法
94
查看次数

如何在python3中模拟文件对象

在 python2 中,我的测试方法中有这样的内容:

mock_file = MagicMock(spec=file)
Run Code Online (Sandbox Code Playgroud)

我正在转向 python3,但我不知道如何进行类似的模拟。我试过了:

from io import IOBase
mock_file = MagicMock(spec=IOBase)

mock_file = create_autospec(IOBase)
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

python mocking python-2.7 python-3.x magicmock

2
推荐指数
1
解决办法
1424
查看次数

从整数类型创建列表时出现类型错误

我正在尝试实现一个简单的功能totient

coprime :: Integral a => a -> a -> Bool
coprime a b = gcd a b == 1

totient :: Integral a => a -> a
totient m = length $ filter (coprime m) [1..m-1]

ghci> :load 99problems.hs
[1 of 1] Compiling Main             ( 99problems.hs, interpreted )

99problems.hs:250:13: error:
    • Couldn't match expected type ‘a’ with actual type ‘Int’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          totient :: forall a. Integral …
Run Code Online (Sandbox Code Playgroud)

haskell integer

1
推荐指数
1
解决办法
117
查看次数

让vs以单调的价值观去做

使用letdo尝试使用Monadic值有什么区别?(不确定这是否是表达它的正确方法)

例如:

--import System.Random

*Main> dupRand = let i = randomRIO (1, 10) in sequence [i, i]
*Main> :t dupRand
dupRand :: (Random a, Num a) => IO [a]
*Main> dupRand
[8,3]
*Main> dupRand
[2,9]
*Main> dupRand
[9,5]

*Main> dupRand2 = do i <- randomRIO (1, 10); pure [i, i]
*Main> :t dupRand2
dupRand2 :: (Random a, Num a) => IO [a]
*Main> dupRand2
[6,6]
*Main> dupRand2
[9,9]
*Main> dupRand2
[5,5]
Run Code Online (Sandbox Code Playgroud)

为什么在中dupRand2,该函数成功复制了一个随机值,而在中dupRand,该函数看起来好像它生成了两个随机值?

random monads haskell let

-1
推荐指数
1
解决办法
99
查看次数