Haskell:从 Data.Random 中的 RVar 中获取值

发布于 2024-12-29 03:34:37 字数 374 浏览 1 评论 0原文

如何编写具有类型签名的函数,例如:

mySample :: StdGen -> Int -> [a] -> [a]

根据

sample :: Int -> [a] -> RVar [a]

Data.Random.Extras?这里,sample 是一个函数,它从给定列表中返回一个随机子列表。

How do I write a function with a type signature like:

mySample :: StdGen -> Int -> [a] -> [a]

in terms of

sample :: Int -> [a] -> RVar [a]

where sample is defined in Data.Random.Extras? Here, sample is a function which returns a random sublist from the given list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2025-01-05 03:34:37

根据文档< /a>,这应该可以工作:

mySample :: StdGen -> Int -> [a] -> [a]
mySample g n xs = fst $ sampleState (sample n xs) g

但是,在尝试编译它时,我遇到了重叠的实例错误。不过,我得到了这个编译:

mySample :: StdGen -> Int -> [a] -> [a]
mySample g n xs = evalState (runRVar (sample n xs) StdRandom) g

According to the documentation, this should work:

mySample :: StdGen -> Int -> [a] -> [a]
mySample g n xs = fst $ sampleState (sample n xs) g

However, I get overlapping instance errors when trying to compile it. I got this to compile, though:

mySample :: StdGen -> Int -> [a] -> [a]
mySample g n xs = evalState (runRVar (sample n xs) StdRandom) g
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文