相关疑难解决方法(0)

哈斯克尔:monadic takeWhile?

我有一些用C语言编写的函数,我从Haskell调用.这些功能返回IO (CInt).有时我想运行所有函数,无论它们返回什么,这很容易.为了示例代码,这是当前正在发生的事情的一般概念:

Prelude> let f x = print x >> return x
Prelude> mapM_ f [0..5]
0
1
2
3
4
5
Prelude>
Run Code Online (Sandbox Code Playgroud)

我得到了我想要的副作用,我不关心结果.但是现在我需要在第一个没有返回我想要的结果的项目之后立即停止执行.假设返回值为4或更高要求执行停止 - 那么我想要做的是:

Prelude> takeWhile (<4) $ mapM f [0..5]
Run Code Online (Sandbox Code Playgroud)

这给了我这个错误:

<interactive>:1:22:
    Couldn't match expected type `[b]' against inferred type `IO a'
    In the first argument of `mapM', namely `f'
    In the second argument of `($)', namely `mapM f ([0 .. 5])'
    In the expression: takeWhile (< 4) $ mapM f ([0 …

monads haskell chaining

12
推荐指数
4
解决办法
2923
查看次数

标签 统计

chaining ×1

haskell ×1

monads ×1