Lethi
Lethi

Reputation: 1177

How can I see the implementation code for "and"?

I have found the definition for and, on the internet but I couldnt find the actual implementation of and. I did search for some sort of prelude haskell file on my computer but it didn't return anything which could be opened in a text editor.

Upvotes: 7

Views: 374

Answers (3)

Sarah
Sarah

Reputation: 6696

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.

Upvotes: 2

Gabriella Gonzalez
Gabriella Gonzalez

Reputation: 35099

You can use Hoogle to search for Haskell functions, like so:

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

The function links take you to the library module where the function was defined, and in this case the link for and takes us to here:

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

You then click on the Source link to the right of the function name and it takes you to the source of that function. For the case of and it links us to here:

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

You'll see that and actually has two definitions. One is the Standard Prelude definition, which is only enabled if you compile with the USE_REPORT_PRELUDE flag, and the other is the definition the Prelude is usually compiled with, which is typically more optimized.

Upvotes: 19

duri
duri

Reputation: 15351

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

and is defined as foldr (&&) True.

Upvotes: 1

Related Questions