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:
You should see a prompt like this:
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:
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:
Practical Example
Let's create a simple Haskell script and run it in GHCi.
-
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)
-
Load the script in GHCi:
Prelude> :load Example.hs [1 of 1] Compiling Example ( Example.hs, interpreted ) Ok, one module loaded. *Example>
-
Call the
double
function:*Example> double 10 20
-
Run the
main
function:*Example> main 10
Exercises
Exercise 1: Simple Arithmetic
- Start GHCi.
- Evaluate the following expressions:
7 * 8
div 10 3
mod 10 3
- Check the type of the expression
7 * 8
.
Solution:
Exercise 2: Loading a Script
- Create a file named
Square.hs
with the following content:module Square where square :: Int -> Int square x = x * x
- Load the script in GHCi.
- Call the
square
function with the argument4
.
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.
Haskell Programming Course
Module 1: Introduction to Haskell
- What is Haskell?
- Setting Up the Haskell Environment
- Basic Syntax and Hello World
- Haskell REPL (GHCi)