Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何对封装在IO monad中的值使用数据构造函数?_Haskell - Fatal编程技术网

Haskell 如何对封装在IO monad中的值使用数据构造函数?

Haskell 如何对封装在IO monad中的值使用数据构造函数?,haskell,Haskell,因此,我在haskell中定义了一个数据类型,如下所示: import qualified Data.UUID as UUID import qualified Data.Time.Clock as CLK data MyRecord = MyRecord { id :: UUID.UUID, creationDate :: CLK.UTCTime, comment :: String } 现在的问题是UUID和UTCTime在IO monad中返回。我用来生成它们的

因此,我在haskell中定义了一个数据类型,如下所示:

import qualified Data.UUID as UUID
import qualified Data.Time.Clock as CLK

data MyRecord = MyRecord {
    id :: UUID.UUID,
    creationDate :: CLK.UTCTime,
    comment :: String
}
现在的问题是UUID和UTCTime在IO monad中返回。我用来生成它们的函数有以下类型

对于UUID

nextRandom :: IO uuid-types-1.0.3:Data.UUID.Types.Internal.UUID
要获取当前时间戳,请执行以下操作:

getCurrentTime :: IO UTCTime
我的问题是如何使用数据构造函数来实际初始化数据类型

我可以做到以下几点

MyRecord <$> nextRandom
这是可行的,但我无法理解如何为构造函数提供另一个参数。

您可以使用顺序应用程序来实现这一点。例如:

MyRecord <$> nextRandom <*> getCurrentTime <*> return "some comment"
这相当于:

some_function :: IO MyRecord
some_function = do
    r <- nextRandom
    t <- getCurrentTime
    return (MyRecord r t "some comment")
这里发生的基本情况是,如果你写:

MyRecord <$> nextRandom
您构造了一个IO UTCTime->String->MyRecord类型的项。现在,通过使用左操作数this函数和右操作数IO UTCTime函数,我们构造了一个类型为IO String->MyRecord的对象

我们可以使用或将字符串转换为IO字符串,因此我们再次使用将其应用于部分数据构造函数。

您可以使用顺序应用程序进行此操作。例如:

MyRecord <$> nextRandom <*> getCurrentTime <*> return "some comment"
这相当于:

some_function :: IO MyRecord
some_function = do
    r <- nextRandom
    t <- getCurrentTime
    return (MyRecord r t "some comment")
这里发生的基本情况是,如果你写:

MyRecord <$> nextRandom
您构造了一个IO UTCTime->String->MyRecord类型的项。现在,通过使用左操作数this函数和右操作数IO UTCTime函数,我们构造了一个类型为IO String->MyRecord的对象

我们可以使用或将字符串转换为IO字符串,因此再次使用将其应用于部分数据构造函数。

这是常见的:

MyRecord nextRandom getCurrentTime 纯粹是我的评论 这是常见的:

MyRecord nextRandom getCurrentTime 纯粹是我的评论