Introduction

The GHCi (Glasgow Haskell Compiler interactive) is an interactive environment for Haskell. It allows you to write and test Haskell code quickly without the need to compile it into an executable. This makes it an invaluable tool for learning, experimenting, and debugging Haskell code.

Key Concepts

What is GHCi?

  • Interactive Shell: GHCi is an interactive shell for Haskell, allowing you to evaluate expressions, load Haskell scripts, and interact with your code in real-time.
  • Immediate Feedback: You can type Haskell expressions and get immediate feedback, which is useful for testing small pieces of code and understanding how they work.

Starting GHCi

To start GHCi, open your terminal and type:

ghci

You should see a prompt like this:

GHCi, version 8.10.4: https://www.haskell.org/ghc/  :? for help
Prelude>

Basic Commands

Here are some basic commands you can use in GHCi:

Command Description
:quit or :q Exit GHCi.
:load or :l Load a Haskell script. Example: :load MyScript.hs
:reload or :r Reload the current script.
:type or :t Show the type of an expression. Example: :type 5
:info or :i Show information about a function or type. Example: :info map
:help or :? Show help information.

Evaluating Expressions

You can type Haskell expressions directly into GHCi to evaluate them. For example:

Prelude> 2 + 3
5

Prelude> map (*2) [1, 2, 3]
[2, 4, 6]

Loading and Running Scripts

You can write Haskell code in a file (e.g., MyScript.hs) and load it into GHCi:

Prelude> :load MyScript.hs
[1 of 1] Compiling Main             ( MyScript.hs, interpreted )
Ok, one module loaded.
*Main>

Once loaded, you can call functions defined in the script:

*Main> myFunction 10
20

Practical Example

Let's create a simple Haskell script and run it in GHCi.

  1. Create a file named Example.hs with the following content:

    module Example where
    
    double :: Int -> Int
    double x = x * 2
    
    main :: IO ()
    main = print (double 5)
    
  2. Load the script in GHCi:

    Prelude> :load Example.hs
    [1 of 1] Compiling Example          ( Example.hs, interpreted )
    Ok, one module loaded.
    *Example>
    
  3. Call the double function:

    *Example> double 10
    20
    
  4. Run the main function:

    *Example> main
    10
    

Exercises

Exercise 1: Simple Arithmetic

  1. Start GHCi.
  2. Evaluate the following expressions:
    • 7 * 8
    • div 10 3
    • mod 10 3
  3. Check the type of the expression 7 * 8.

Solution:

Prelude> 7 * 8
56

Prelude> div 10 3
3

Prelude> mod 10 3
1

Prelude> :type 7 * 8
7 * 8 :: Num a => a

Exercise 2: Loading a Script

  1. Create a file named Square.hs with the following content:
    module Square where
    
    square :: Int -> Int
    square x = x * x
    
  2. Load the script in GHCi.
  3. Call the square function with the argument 4.

Solution:

Prelude> :load Square.hs
[1 of 1] Compiling Square           ( Square.hs, interpreted )
Ok, one module loaded.
*Square> square 4
16

Common Mistakes and Tips

  • Forgetting to Save the Script: Always save your Haskell script before loading or reloading it in GHCi.
  • Syntax Errors: GHCi will provide error messages if there are syntax errors in your script. Read the messages carefully to understand what needs to be fixed.
  • Type Errors: If you encounter type errors, use the :type command to check the types of your expressions and ensure they match the expected types.

Conclusion

GHCi is a powerful tool for Haskell programmers, providing an interactive environment to test and debug code quickly. By mastering GHCi commands and understanding how to load and run scripts, you can significantly enhance your Haskell development workflow. In the next module, we will dive into basic Haskell concepts, starting with variables and functions.

© Copyright 2024. All rights reserved