Getting Started

Chapter Goals

In this chapter, we'll set up a working PureScript development environment, solve some exercises, and use the tests provided with this book to check our answers. You may also find a video walkthrough of this chapter helpful if that better suits your learning style.

Environment Setup

First, work through this Getting Started Guide in the Documentation Repo to setup your environment and learn a few basics about the language. Don't worry if the code in the example solution to the Project Euler problem is confusing or contains unfamiliar syntax. We'll cover all of this in great detail in the upcoming chapters.

Editor support

You can use your preferred editor to write PureScript (for example, to solve the book exercises). See Editor Support Documentation.

Note that some editors expect a spago.dhall file in the root of the opened project for full IDE support. For example, you should open the chapter2 directory to work on the exercises in this chapter.

If you use VS Code, you can use the provided workspace to open all the chapters simultaneously.

Solving Exercises

Now that you've installed the necessary development tools, clone this book's repo.

git clone https://github.com/purescript-contrib/purescript-book.git

The book repo contains PureScript example code and unit tests for the exercises that accompany each chapter. There's some initial setup required to reset the exercise solutions so they are ready to be solved by you. Use the prepareExercises.sh script to simplify this process:

cd purescript-book
./scripts/prepareExercises.sh
git add .
git commit --all --message "Exercises ready to be solved"

Now run the tests for this chapter:

cd exercises/chapter2
spago test

You should see the following successful test output:

→ Suite: Euler - Sum of Multiples
  ✓ Passed: below 10
  ✓ Passed: below 1000

All 2 tests passed! 🎉

Note that the answer function (found in src/Euler.purs) has been modified to find the multiples of 3 and 5 below any integer. The test suite (located in test/Main.purs) for this answer function is more comprehensive than the test in the earlier getting-started guide. Don't worry about understanding how this test framework code works while reading these early chapters.

The remainder of the book contains lots of exercises. If you write your solutions in the Test.MySolutions module (test/MySolutions.purs), you can check your work against the provided test suite.

Let's work through this next exercise together in a test-driven-development style.

Exercise

  1. (Medium) Write a diagonal function to compute the length of the diagonal (or hypotenuse) of a right-angled triangle when given the lengths of the two other sides.

Solution

We'll start by enabling the tests for this exercise. Move the start of the block-comment down a few lines, as shown below. Block comments start with {- and end with -}:

    suite "diagonal" do
      test "3 4 5" do
        Assert.equal 5.0 (diagonal 3.0 4.0)
      test "5 12 13" do
        Assert.equal 13.0 (diagonal 5.0 12.0)
    {-  Move this block comment starting point to enable more tests

If we attempt to run the test now, we'll encounter a compilation error because we have not yet implemented our diagonal function.

$ spago test

Error found:
in module Test.Main
at test/Main.purs:21:27 - 21:35 (line 21, column 27 - line 21, column 35)

  Unknown value diagonal

Let's first look at what happens with a faulty version of this function. Add the following code to test/MySolutions.purs:

import Data.Number (sqrt)

diagonal w h = sqrt (w * w + h)

And check our work by running spago test:

→ Suite: diagonal
  ☠ Failed: 3 4 5 because expected 5.0, got 3.605551275463989
  ☠ Failed: 5 12 13 because expected 13.0, got 6.082762530298219

2 tests failed:

Uh-oh, that's not quite right. Let's fix this with the correct application of the Pythagorean formula by changing the function to:

diagonal w h = sqrt (w * w + h * h)

Trying spago test again now shows all tests are passing:

→ Suite: Euler - Sum of Multiples
  ✓ Passed: below 10
  ✓ Passed: below 1000
→ Suite: diagonal
  ✓ Passed: 3 4 5
  ✓ Passed: 5 12 13

All 4 tests passed! 🎉

Success! Now you're ready to try these next exercises on your own.

Exercises

  1. (Easy) Write a function circleArea which computes the area of a circle with a given radius. Use the pi constant, which is defined in the Numbers module. Hint: don't forget to import pi by modifying the import Data.Number statement.
  2. (Medium) Write a function leftoverCents which takes an Int and returns what's leftover after dividing by 100. Use the rem function. Search Pursuit for this function to learn about usage and which module to import it from. Note: Your IDE may support auto-importing of this function if you accept the auto-completion suggestion.

Conclusion

In this chapter, we installed the PureScript compiler and the Spago tool. We also learned how to write solutions to exercises and check these for correctness.

There will be many more exercises in the chapters ahead, and working through those helps with learning the material. If any of the exercises stumps you, please reach out to any of the community resources listed in the Getting Help section of this book, or even file an issue in this book's repo. This reader feedback on which exercises could be made more approachable helps us improve the book.

Once you solve all the exercises in a chapter, you may compare your answers against those in the no-peeking/Solutions.purs. No peeking, please, without putting in an honest effort to solve these yourself. And even if you are stuck, try asking a community member for help first, as we would prefer to give you a small hint rather than spoil the exercise. If you found a more elegant solution (that only requires knowledge of the covered content), please send us a PR.

The repo is continuously being revised, so be sure to check for updates before starting each new chapter.