小编McB*_*den的帖子

Haskell ReaderT设计模式与MTL StateT模式

我正在设计一个基本上使用StateT并只是更新状态的小型游戏。以下是简化版本:

{-# LANGUAGE TemplateHaskell #-}

import           Control.Lens
import           Control.Monad
import           Control.Monad.IO.Class
import           Control.Monad.State
import           Control.Monad.State.Class
import           System.Random

data PlayerState = PlayerState {
  _psName  :: String,
  _psScore :: Int
                               } deriving (Show)

makeClassy ''PlayerState

data Game = Game {
  _turns   :: Int,
  _players :: [PlayerState]
                 } deriving (Show)

makeClassy ''Game

randomGameInit :: IO Game
randomGameInit = do
  players <- replicateM 5 $  PlayerState <$> (replicateM 4 $ randomRIO ('a', 'z')) <*> randomRIO (1,10)
  return $ Game 0 players

update :: (MonadState …
Run Code Online (Sandbox Code Playgroud)

haskell

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

自定义快速检查失败消息

test1 = hspec $ do
  describe "blabla" $ do
    it "should be equl" $ verbose $
      \input-> ...
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,当测试失败时,它会打印失败的input. 但我实际上对可以从 计算出的另一个值感兴趣input。我可以要求QuickCheck打印其他值吗?

haskell quickcheck

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

为什么我的并行代码比没有并行的代码还要慢?

我按照 Simon Marlow 的关于并行 Haskell 的书(第 1 章)使用rpar/ rseq

下面是代码(解决Squid Game桥模拟):

{-# LANGUAGE FlexibleContexts #-}

import Control.DeepSeq (force)
import Control.Exception (evaluate)
import Control.Parallel.Strategies
import Data.Array.IO
  ( IOUArray,
    getAssocs,
    newListArray,
    readArray,
    writeArray,
  )
import Data.Functor ((<&>))
import System.Environment (getArgs)
import System.Random (randomRIO)

game ::
  Int -> -- number of steps
  Int -> -- number of glass at each step
  Int -> -- number of players
  IO Int -- return the number of survivors
game totalStep totalGlass = go …
Run Code Online (Sandbox Code Playgroud)

haskell

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

如何理解Haskell函数参数

在以下函数定义中:

app :: Application
app _ respond = do
    putStrLn "I've done some IO here"
    respond $ responseLBS
        status200
        [("Content-Type", "text/plain")]
        "Hello, Web!"
Run Code Online (Sandbox Code Playgroud)

您好,app函数似乎没有参数。为什么在上面的示例中有两个参数?

haskell signature

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

How to read/parse Int from String

I use read "123" :: Int to read in Int from String. This works great for a simple case such as "123"; But it fails when the input is "12.34".

What do people usually do in such cases?

Edit: I also would like to know if there is a simple way to parse numbers with thousand separator such as "12,345"

haskell

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

如何使用inline-c包装将指针返回到自定义结构类型的函数?

我计划使用inline-c包装C函数:

lxw_workbook  *workbook  = workbook_new("filename.xlsx");
Run Code Online (Sandbox Code Playgroud)

我需要捕获返回的Ptr,以便以后在其他函数中使用。

问题:Ptr指向lxw_workbook,这是一个自定义结构。我是否需要为此Haskell类型编写一个存储实例?因为如果我不直接使用该结构,而仅使用Ptr,可以跳过此步骤吗?

haskell

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

QuickCheck limit to only certain data constructor

I have a data type definition:

data Point = Point {x :: Int, h :: Int} | EmptyPoint
Run Code Online (Sandbox Code Playgroud)

In my property test, I would like to limit the test only on the Point constructor cases. For example point1 - point2 = Point 0 0. This presumes that the accessor x is defined which is not the case with EmptyPoint.

in other words: I don't want EmptyPoint to be generated.

Is there a way to do that?

haskell

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

如何理解MonadWriter Typeclass中的递归函数调用

mtl库中,我看到了以下定义MonadWriter:

class (Monoid w, Monad m) => MonadWriter w m | m -> w where
#if __GLASGOW_HASKELL__ >= 707
    {-# MINIMAL (writer | tell), listen, pass #-}
#endif
    -- | @'writer' (a,w)@ embeds a simple writer action.
    writer :: (a,w) -> m a
    writer ~(a, w) = do
      tell w
      return a

    -- | @'tell' w@ is an action that produces the output @w@.
    tell   :: w -> m ()
    tell w = writer ((),w)
Run Code Online (Sandbox Code Playgroud)

所以 …

haskell

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

标签 统计

haskell ×8

quickcheck ×1

signature ×1