正在使用mapM /序列被认为是良好的做法? [英] Is using mapM/sequence considered good practice?

查看:131
本文介绍了正在使用mapM /序列被认为是良好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下例子:

Consider following example:

safeMapM f xs = safeMapM' xs []
    where safeMapM' []     acc = return $ reverse acc
          safeMapM' (x:xs) acc = do y <- f x
                                    safeMapM' xs (y:acc)

mapM return largelist      -- Causes stack space overflow on large lists
safeMapM return largelist  -- Seems to work fine

使用 mapM 在大型列表中导致堆栈空间溢出,而 safeMapM 似乎正常工作(使用GHC 7.6.1和 -O2 )。然而,我无法在Haskell标准库中找到类似于 safeMapM 的函数。

Using mapM on large lists causes a stack space overflow while safeMapM seems to work fine (using GHC 7.6.1 with -O2). However I was not able to find a function similar to safeMapM in the Haskell standard libraries.

是否仍然考虑它对于这个问题,最好使用 mapM (或者 sequence

如果是,为什么它被认为是良好的做法,尽管存在堆栈空间溢出的危险?

如果不是您建议使用哪种替代方法?

Is it still considered good practice to use mapM (or sequence for that matter)?
If so, why is it considered to be good practice despite the danger of stack space overflows?
If not which alternative do you suggest to use?

推荐答案

编辑:换句话说,尽管问题中提出了问题,但是 mapM 序列是常用的,通常被认为是最佳做法。

in other words, despite the issue raised in the question, yes, mapM and sequence are commonly used and typically considered good practice.

这篇关于正在使用mapM /序列被认为是良好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆