Haskell/Using GHCi effectively

GHCi assists in several ways toward more efficient work. Here, we will discuss some of the best practices for using GHCi.

Tab completion
As in many other terminal programs, you can enter some starting text in GHCi and then hit the Tab key to be presented with a list of all possibilities that start with what you've written so far. When there is only one possibility, using Tab will auto-complete the string. For example  will append letter "d" (since nothing exists with "fol" other than items that start with "fold"). A second Tab will list the four functions included in Prelude:,  ,  , and. More options may show if you have already imported additional modules.

Tab completion works also when you are loading a file with your program into GHCi. For example, after typing , you will be presented with all files that start with "fi" that are present in the current directory (the one you were in when you launched GHCi).

The same also applies when you are importing modules, after typing  or  , you will be presented with all modules that start with "Da" present in installed packages.

": commands"
On GHCi command line, commands for the interpreter start with the character ":" (colon).
 * or  -- prints a list of all available commands.
 * or  -- loads a given file into GHCi (you must include the filename with the command).
 * or  -- reloads whatever file had been loaded most recently (useful after changes to the file).
 * or  -- prints the type of a given expression included with the command
 * or  -- loads a given module (include the module name with the command). You can also unload a module by adding a   symbol before the module name.
 * -- gives the type signatures for all functions available from a given module.

Here again, you can use Tab to see the list of commands, type :<tt>Tab</tt> to see all possible commands.

Timing Functions in GHCi
GHCi provides a basic way to measure how much time a function takes to run, which can be useful for to find out which version of a function runs fastest (such as when there are multiple ways to define something to get the same effective result).
 * 1) Type   into the ghci command line.
 * 2) run the function(s) you are testing. The time the function took to run will be displayed after GHCi outputs the results of the function.

Multi-line Input
If you are trying to define a function that takes up multiple lines, or if you want to type a do block into ghci (without writing a file that you then import), there is an easy way to do this:
 * 1) Begin a new line with
 * 2) Type in your code. Press enter when you need a new line.
 * 3) Type   to end the multi-line input.

For example:

*Main> :{ *Main| let askname = do   *Main|               putStrLn "What is your name?" *Main|              name <- getLine *Main|              putStrLn $ "Hello " ++ name *Main| :} *Main>

The same can be accomplished by using  command (allow multi-line commands). In this case, an empty line will end the block.

In addition, line breaks in ghci commands can be separated by, like this:

*Main> let askname1 = do ; putStrLn "what is your name?" ; name <- getLine ; putStrLn $ "Hello " ++ name