本文介绍了不进行任何过滤并仅打开包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用该程序时遇到了麻烦.

I'm having trouble with this program.

filterJust :: [Maybe a] -> [a]

filterJust [] = []
filterJust x = map fromJust (filter (isJust) x)

但是ghci一直在报告此事

but ghci keeps reporting this

我不想使用任何其他模块,所以我做到了:

I don't want to use any additional module so i made this:

filterJust :: [Maybe a] -> [a]

filterJust x = map unpack (filter (Nothing /=) x)

unpack (Just a) = a

我收到此消息

我不明白为什么.我应该能够使用Eq函数而无需导入其他内容了?

and i dont understand why. I should be able to use Eq functions without importing anthing right?

推荐答案

您不需要编写filterJust函数.它已经在base中,并且称为catMaybes: http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Maybe.html#v:catMaybes

You don't need to write filterJust function. It is already in base and it is called catMaybes: http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Maybe.html#v:catMaybes

此外,您可以看到定义此功能的更好方法:

Also, you can see better way to define this function:

catMaybes :: [Maybe a] -> [a]
catMaybes ls = [x | Just x <- ls]

因此,您只需将import Data.Maybe (catMaybes)添加到您的模块中即可.

So all you need to do is just add import Data.Maybe (catMaybes) into your module.

此处,此功能使用"MonadFail sugar"(单糖).列表的设计模式.您可以在以下博客文章中了解有关它和其他模式的更多信息:

Here this function is using the "MonadFail sugar" design pattern for lists. You can read more about it and other patterns in the following blog post:

这篇关于不进行任何过滤并仅打开包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:10