7

我在互联网上找到了 and 的定义,但我找不到 and 的实际实现。我确实在我的计算机上搜索了某种 prelude haskell 文件,但它没有返回任何可以在文本编辑器中打开的内容。

4

3 回答 3

19

您可以使用Hoogle搜索 Haskell 函数,如下所示:

http://www.haskell.org/hoogle/?hoogle=and

函数链接将您带到定义函数的库模块,在这种情况下,链接and将我们带到这里:

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:and

然后单击Source函数名称右侧的链接,它会将您带到该函数的源代码。对于它的情况,and我们链接到这里:

http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-List.html#and

and                     :: [Bool] -> Bool
or                      :: [Bool] -> Bool
#ifdef USE_REPORT_PRELUDE
and                     =  foldr (&&) True
or                      =  foldr (||) False
#else
and []          =  True
and (x:xs)      =  x && and xs
or []           =  False
or (x:xs)       =  x || or xs
#endif

你会看到它and实际上有两个定义。一种是标准 Prelude 定义,只有在使用标志编译时才启用USE_REPORT_PRELUDE,另一种是 Prelude 通常编译时使用的定义,通常更优化。

于 2012-09-22T14:57:19.990 回答
2

You can't expect to find the source code in your local distribution of Haskell - which is probably Haskell Platform. Instead, go to Hoogle and search for the function. One way, which is what you want, is to search by its name, and, go to its Haddock, and then click "source."

On Hoogle, you can also search by type signature, ie. [Bool] -> Bool which you'll probably find yourself doing quite often.

于 2012-09-22T14:57:56.917 回答
1

看看http://www.haskell.org/onlinereport/standard-prelude.html

and定义为foldr (&&) True

于 2012-09-22T14:54:47.183 回答