|
| 1 | +--- |
| 2 | +title: Get Started |
| 3 | +page: get-started |
| 4 | +isGettingStarted: true |
| 5 | +--- |
| 6 | + |
| 7 | +# Get started |
| 8 | + |
| 9 | +Welcome, new Haskeller! Read on to quickly set up your Haskell development environment, execute your first lines of code, and get directions for further learning. |
| 10 | + |
| 11 | +## Content |
| 12 | + - [Set up a Haskell development environment](#set-up-a-haskell-development-environment) |
| 13 | + - [GHCup: universal installer](#ghcup-universal-installer) |
| 14 | + - [Editor](#editor) |
| 15 | + - [Running your first lines of code](#running-your-first-lines-of-code) |
| 16 | + - [Writing your first Haskell program](#writing-your-first-haskell-program) |
| 17 | + - [Participate in the community](#participate-in-the-community) |
| 18 | + - [Next steps](#next-steps) |
| 19 | + |
| 20 | +## Set up a Haskell development environment |
| 21 | + |
| 22 | +A complete Haskell development environment consists of the Haskell toolchain (compiler, language server, build tool) and an editor with good Haskell support. The quickest way to get this set up is to: |
| 23 | + |
| 24 | +1. Use **GHCup** to install and manage the Haskell toolchain. |
| 25 | +2. Use [**VSCode**](https://code.visualstudio.com/) as the editor, with the Haskell extension installed. |
| 26 | + |
| 27 | +### GHCup: universal installer |
| 28 | + |
| 29 | +[GHCup](https://www.haskell.org/ghcup/#) is a universal installer for Haskell that will install everything you need to program in Haskell, and will help you manage those installations (update, switch versions, ...). Follow the instructions on the [GHCup webpage](https://www.haskell.org/ghcup/#) to install GHCup. Then use it to install the Haskell toolchain, which consists of: |
| 30 | + |
| 31 | +1. **GHC** (The Haskell compiler) We will use GHC below to run our examples, but in practice you will mostly use a tool like Cabal or Stack to build your code, instead of GHC directly. |
| 32 | +2. **HLS** (The Haskell Language Server) You won't use HLS directly, instead your code editor will use it in the background to provide you with a great experience while editing Haskell code. |
| 33 | +3. **Cabal** (A Haskell build tool) You will use Cabal to structure your Haskell projects, build them, run them, define dependencies, ... . |
| 34 | +4. **Stack** (A Haskell build tool) An alternative to Cabal. |
| 35 | + |
| 36 | + |
| 37 | +<div class="bs-callout bs-callout-info"> |
| 38 | + <p> |
| 39 | + <h4>Cabal and Stack: which one should I install?</h4> |
| 40 | + We recommend installing both. Most Haskell projects can be built using Cabal, but some might require Stack. Installing both guarantees that you can use either, and while following a tutorial or book you can use whatever they recommend. |
| 41 | + </p> |
| 42 | +</div> |
| 43 | + |
| 44 | +### Editor |
| 45 | +[**Visual Studio Code**](https://code.visualstudio.com/) ([**VSCode**](https://code.visualstudio.com/)) is a popular choice with well-supported Haskell integration. Install the [Haskell extension](https://marketplace.visualstudio.com/items?itemName=haskell.haskell) and you are all set. It should work out of the box and use your installation of HLS. To learn about support for other editors, check out [HLS docs for editor configuration](https://haskell-language-server.readthedocs.io/en/latest/configuration.html#configuring-your-editor). |
| 46 | + |
| 47 | +## Running your first lines of code |
| 48 | + |
| 49 | +We have everything set up, let's use it! The Haskell compiler, GHC, comes with an interactive interpreter called GHCi which is great for playing with Haskell and trying things out, so let's give it a spin. Run `ghci` at your command prompt, which will start a new GHCi prompt for you. Let's do a simple calculation to check Haskell's computing capabilities: |
| 50 | +``` |
| 51 | +> 6 + 3^2 * 4 |
| 52 | +42 |
| 53 | +``` |
| 54 | + |
| 55 | +42 is a nice even number, but what about the first 10 even numbers after it? |
| 56 | +``` |
| 57 | +> take 10 (filter even [43..]) |
| 58 | +[44,46,48,50,52,54,56,58,60,62] |
| 59 | +``` |
| 60 | + |
| 61 | +What is the sum of those? |
| 62 | +``` |
| 63 | +> sum it |
| 64 | +530 |
| 65 | +``` |
| 66 | +**NOTE**: We used a special feature of GHCi here, which is a special `it` variable that remembers the result of the last expression. |
| 67 | + |
| 68 | +Great, you got the first taste of Haskell! Now let's get to running a real program. |
| 69 | + |
| 70 | +## Writing your first Haskell program |
| 71 | + |
| 72 | +In your editor, create a new file named `hello.hs`. Write the following in it: |
| 73 | +```hs |
| 74 | +main = do |
| 75 | + putStrLn "Hello, everybody!" |
| 76 | + putStrLn ("Please look at my favorite odd numbers: " ++ show (filter odd [10..20])) |
| 77 | +``` |
| 78 | + |
| 79 | +You can now compile it with `ghc` to produce an executable called `hello` that we will then run: |
| 80 | +``` |
| 81 | +> ghc hello.hs |
| 82 | +> ./hello |
| 83 | +Hello, everybody! |
| 84 | +Please look at my favorite odd numbers: [11,13,15,17,19] |
| 85 | +``` |
| 86 | + |
| 87 | +There you go, you just wrote a short, polite program in Haskell! |
| 88 | + |
| 89 | +**GHCI TIP**: You can also load your file directly into `ghci`, which will enable you to play with any functions and other definitions you defined in it. So for our example, we can just load `hello.hs` with GHCi and then call the function `main` like this: |
| 90 | +``` |
| 91 | +> ghci hello.hs |
| 92 | +GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help |
| 93 | +[1 of 1] Compiling Main ( hello.hs, interpreted ) |
| 94 | +Ok, one module loaded. |
| 95 | +> main |
| 96 | +Hello, everybody! |
| 97 | +Please look at my favorite odd numbers: [11,13,15,17,19] |
| 98 | +``` |
| 99 | + |
| 100 | +## Participate in the community |
| 101 | + |
| 102 | +By participating in the Haskell community you be able to ask for help and learn about new developments in the Haskell ecosystem. Some of the most popular places to interact with the community are: |
| 103 | + |
| 104 | + - [Haskell Discourse](https://discourse.haskell.org/) |
| 105 | + - [Haskell subreddit](https://www.reddit.com/r/haskell/) |
| 106 | + - [https://wiki.haskell.org/IRC_channel](https://wiki.haskell.org/IRC_channel) |
| 107 | + |
| 108 | +We recommend joining right now, and don't be shy to ask for help! Check [https://www.haskell.org/community](https://www.haskell.org/community) for a full list of resources relating to the Haskell community. |
| 109 | + |
| 110 | +## Next steps |
| 111 | + |
| 112 | +Popular free learning resources for beginners: |
| 113 | + |
| 114 | + - [Introductory Haskell course of the University of Pennsylvania (CIS194)](https://www.seas.upenn.edu/~cis1940/spring13/lectures.html) (course) |
| 115 | + - [Learn You a Haskell for Great Good!](http://learnyouahaskell.com/) (book) |
| 116 | + - [Learn Haskell by building a blog generator](https://lhbg-book.link) (book) |
| 117 | + |
| 118 | +This is just the tip of the iceberg though: check [Documentation](https://www.haskell.org/documentation/) for a bigger list of learning resources. That is it, fellow Haskeller! Enjoy learning Haskell, do (not?) be lazy and see you in the community! |
0 commit comments