Programming Languages Section

I Want to Know About the PureScript Language

What is PureScript Langugage? - website post card
Post by Amina Delali, January 17th, 2023

Some Facts

Purescript is a purely functional , strongly typed programming language, can be compiled to Javascript, and can be used to develop real world applications. So, it can be used for developing web applications, web servers, and desktop applications using Electron as well as for, templating languages and extensions.

Even if Purescript is written in, and inspired by Haskell, it brings its own features like row polymorphism and extensible records. And it also adheres to a strict evaluation strategy.

It is recommended to provide explicit types signatures while writing with the language. However, Purescript can infer the types automatically. The language is also able to apply the code elimination process (deleting unused code) while compiling to Javascript.

Some of the companies, already used Purescript. For example, there is lumi, ARoW Co., Ltd, and FOAM. For an extended list, you can check the page here.



How to install it

  • on Windows:

    There are released binaries that you can download and install. However, you are going to need to use Spago tool, to build and manage your packages. You will also need to install esbuild to bundle your project into one single js file.

    In order to install everything, you will have to first install node.js and npm. So, in the following, all the necessary steps, that you have to go through, in order to install and start working with Purescript:

    1. You can install node.js at the same time as npm, by downloading and running the msi installer available from the node.js official download page. To check the installation, open the command prompt, and run these two commands (one after the other): node -v
      npm -v
    2. Now you can install Purescript using npm. On the command prompt run: npm install -g purescript Run this command to check the installation: purs
    3. To install Spago; the Purescript build tool and the package manager, open the command prompt, and run the following command: npm install -g spago
    4. To install the bundler esbuild, run this command: npm install -g esbuild
  • on ubuntu :

      As for Windows 10, to install Purescript on Ubuntu, you will have to install node.js, and npm. After installing purescript, you will need to install its build tool and package manager Spago:

      1. To install node.js, and npm on Ubuntu, open the terminal, and run the following commands: sudo apt update
        sudo apt upgrade
        sudo apt install nodejs
        sudo apt install npm
      2. To install purescript, type this command on the terminal: npm install -g purescript To check the installation, run: purs
      3. To install Spago on Ubuntu, run this command: npm install -g spago
      4. To install the bundler esbuild on Ubuntu, run this command: npm install -g esbuild


The Hello World Example

You can use the purescript repl to load your written code, and run it. You can also, create and build a new project using Spago. We are going with the second option

  1. Create a new folder, then initiate a new Purescript project in that folder. On the terminal (or on the command prompt) run these commands: mkdir Hello
    cd Hello
    spago init
    The initiation process will create new files and folder, one of them is the src folder.
  2. Create a new file, named Hello.purs, and save it in the src folder.
  3. Write the following code in that created file:

    module Hello where
    import Prelude

    greet :: String -> String
    greet name "Hello, " <> name <> "!"

    answer greet "World"

  4. Open the Main.purs file in the same src folder, and change it to this:

    module Main where

    import Prelude

    module Effect (Effect)
    import Hello (answer)
    import Effect.Console (log)

    main :: Effect Unit
    main do
      log (show answer)

  5. Now, write this on the terminal (or the command prompt) to compile and run the code: spago run If you want to only build the code, run: spago build
  6. To compile the code to Javascript, to run with the browser, use this command: spago bundle-app
  7. Now, you can incorporate your js file in an html document, and open it in the browser. For example, you can create this file, and save it as Hello.html in the Hello project folder created earlier:

    <html>

      <head>
        <meta charset="UTF-8">
        <title>Hello World</title>
      </head>

      <body>
        <script src="./index.js"></script>
      </body>

    </html>

  8. Open your html file from your brwoser, you will see the Hello, Wold! message printed in the console.

Additional Information

Something to say ?

If you want to add something about the language or about this post, please feel free to do it by commenting below 🙂 .