CS计算机代考程序代写 Haskell # Tutorial 08

# Tutorial 08

This tutorial will go over a brief overfiew of the “initial” and “final” (typed tagless final) language encodings, and create a “pretty printer” of sorts using the `pretty` library.

## `pretty`

### What is `pretty`?

`pretty` is a “pretty-printing” library that allows you to format your text easily in Haskell!

From their [github page](https://github.com/haskell/pretty); “It is based on the pretty-printer outlined in the paper ‘The Design of a Pretty-printing Library’ by John Hughes in Advanced Functional Programming, 1995. It can be found here.”

### Installing `pretty`

1. Prepare a new stack project and `cd` into it.
`stack new Tutorial08`
`cd Tutorial08`
2. Open up your `project.yaml` file and add `pretty` to your list of dependencies.

You’re all done! 🙂

To validate installation worked, you can import `Text.PrettyPrint` in your `app/Main.hs` file, and run `stack run` to make sure no errors occur.

### Using `pretty`

You can find some examples of usage on their [github page](https://github.com/haskell/pretty), alongside the source code, and code documentation.

Have a look at [src/Text/PrettyPrint.hs](https://github.com/haskell/pretty/blob/master/src/Text/PrettyPrint.hs) for a list of available functions, and [src/Text/PrettyPrint/Annotated/HughesPJ.hs](https://github.com/haskell/pretty/blob/master/src/Text/PrettyPrint/Annotated/HughesPJ.hs) for the same functions list but with examples and documentation.

You essentially convert things into `Doc`s and create connections between them (such as `nest`ing, punctuation, brackets, concatenation, etc), and then `render` your final `Doc` into a String for printing.