Skip to content

Code Listing

It may sometimes1 be useful to view the definitions of functions directly from GHCi. For this, we have the :list command. Note, however, that this command only works for "interpreted" code. What does this mean? I mentioned that Haskell is a compiled language. All the standard library modules you may use in your code are stored in compiled form and are not interpreted. Thus, trying to get the listing of the map function, for example, doesn't work:

GHCi
>>> :list map
cannot list source code for map: module GHC.Base is not interpreted

Files loaded using :load are interpreted, so :list can show you their source code. For example, after loading the MergeSort.hs file used before, we can inspect the various functions it defines:

GHCi
>>> :l MergeSort
[1 of 1] Compiling MergeSort        ( MergeSort.hs, interpreted )
Ok, one module loaded.
>>> :list mergeSort
3  mergeSort :: Ord a => [a] -> [a]
4  mergeSort [] = []
5  mergeSort xs = mergeInPairs [[x] | x <- xs]
>>> :list merge
15  merge :: Ord a => [a] -> [a] -> [a]
16  merge []         ys         = ys
17  merge xs         []         = xs

  1. I say "sometimes" because this works only for interpreted code, that is, essentially only for code you've written yourself, in which case I personally find it more convenient to simply open the source code file and have a look at the function definition in the bigger context where it is defined. It then also allows me to directly edit the function definition if I'm not happy with it.