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