Haskell — how to install and some notes on Uses
Numeric types, types classes, web development
Haskell Programming Language — How to Install
To install Haskell, you can follow these steps depending on your operating system:
### Windows
1. Go to the official Haskell website [here](https://www.haskell.org/platform/windows.html) and download the installer according to your system’s architecture.
2. Run the setup file and install it like any other Windows application.
3. You may need to update the CABAL configuration of your system.
### Linux (Ubuntu)
1. Open a terminal by pressing `Ctrl + Alt + T`.
2. Run the following command to install the Haskell Platform:
$ sudo apt-get install haskell-platform
3. After installation, type `ghc` on the terminal and hit Enter. This should prompt whether you want to install the GHCi interpreter or not. Type `Y` and hit Enter.
4. Install the Cabal build tool by running these commands:
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:hvr/ghc
$ sudo apt install cabal-install
### Mac
1. Go to the official Haskell website [here](https://www.haskell.org/platform/mac.html) and download the installer according to your system’s architecture.
2. Run the setup file and install it like any other Mac application.
### Setting up a Haskell development environment
For a complete Haskell development environment, it is recommended to use Visual Studio Code (VSCode) as the editor, with the Haskell extension installed. Follow these steps:
1. Install [VSCode](https://code.visualstudio.com/).
2. Install the [Haskell extension](https://marketplace.visualstudio.com/items?itemName=haskell.haskell) from the VSCode marketplace.
3. Install GHCup by running the following command in the terminal:
$ curl — proto ‘=https’ — tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
4. Follow the prompts to add the required configurations to your shell config files and install Haskell Language Server and stack.
5. Create a new Haskell project using stack by running these commands:
$ stack new vscode-haskell-config
$ cd vscode-haskell-config
$ stack setup
Now you should have a complete Haskell development environment set up on your system.
Sources:
- [Source 1](https://www.haskell.org/get-started/)
- [Source 2](https://www.freecodecamp.org/news/haskell-programming-language-introduction/)
- [Source 3](https://wiki.haskell.org/Haskell_in_5_steps)
- [Source 6](https://stackoverflow.com/questions/304614/haskell-on-windows-setup)
- [Source 7](https://betterprogramming.pub/haskell-vs-code-setup-in-2021-6267cc991551)
- [Source 9](https://www.tutorialspoint.com/haskell/haskell_environment_setup.htm)
Basics of Haskell Syntax
Haskell is a fully functional programming language that supports lazy evaluation and type classes. It forces developers to write very correct code and is the quintessential nature of the language [Source 6]. Functions are first-class, meaning they can be passed in as arguments to other functions, returned from functions, assigned from variables, and held in data structures, such as lists [Source 4]. Anonymous functions are wrapped by parentheses. In Haskell, functions are applied to arguments by writing the function first, followed by a space, followed by the argument.
Haskell is based on the lambda calculus, which is why the lambda symbol is used in Haskell’s logo [Source 9]. When working with functions, Haskell has an innovative type system which supports a systematic form of overloading and a module system. Haskell has an expressive syntax and a rich variety of built-in data types, including arbitrary-precision integers and rationals, as well as the more conventional integer, floating-point, and boolean types [Source 9]. Haskell’s syntax construction functions directly relate to the syntax constructors from the algebraic data types Exp, Pat, Dec, and Type for representing Haskell code. They hide the monadic nature of building object programs and construct a TH object program directly in Q, thus freeing the API consumer from doing the monadic wrapping and unwrapping manually [Source 12].
Numeric Types
In Haskell, numeric types are organized in a typeclass hierarchy. Some of the most common numeric typeclasses are `Num`, `Real`, and `Integral` ([Source 1].
- `Num`: a typeclass that includes all numeric types, such as `Int`, `Integer`, `Float`, and `Double`.
- `Real`: a typeclass covering the types that can be represented as a real value (the `Rational` type).
- `Integral`: a typeclass for integral types like `Int`, `Integer`, and `Word`.
Some common numeric types in Haskell are:
- `Int`: a fixed-width integral type, at least 29 bits.
- `Integer`: an unbounded integral type, capable of representing arbitrarily large integers.
- `Float`: a single-precision floating point type.
- `Double`: a double-precision floating point type.
- `Rational`: a ratio of two integers, represented as `Ratio Integer`.
Here’s an example of using these numeric types:
haskell
num1 :: Int
num1 = 42
num2 :: Integer
num2 = 12345678901234567890
num3 :: Float
num3 = 3.14
num4 :: Double
num4 = 2.718281828459045
num5 :: Rational
num5 = 1 % 3
You can also use the `:info` GHCi command to learn more about typeclasses and their instances ([Source 12](https://tgdwyer.github.io/haskell2/)).
Custom functions
To create a custom function in Haskell, you can define the function with its type signature and implementation. For example, consider a function `add` that takes two integers as input and returns their sum:
```haskell
add :: Integer -> Integer -> Integer
add x y = x + y
```
Here, `add` has a type signature `Integer -> Integer -> Integer`, which means it takes two integers as input and returns an integer as output. The implementation `add x y = x + y` specifies how the function works ([Source 2](https://www.haskell.org/tutorial/functions.html)).
You can also create higher-order functions that take functions as input or output arguments. For example, consider the `map` function that applies a given function to each element of a list:
```haskell
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map func (x : abc) = func x : map func abc
```
Here, `map` takes a function `func` of type `(a -> b)` as its first argument, and a list of type `[a]` as its second argument. It returns a list of type `[b]`, where each element is the result of applying `func` to the corresponding element in the input list ([Source 5](https://www.tutorialspoint.com/haskell/haskell_functions.htm)).
Why use Haskell?
Numerous times this has been answered. Google it and see what you find.
Custom Data Types and Type Classes
To create custom data types in Haskell, you can use the `data` keyword followed by the type constructor and value constructors separated by the pipe `|` symbol. For example, to create a simple custom data type `Foo`, you can write:
```haskell
data Foo = Bar | Biz
```
Here, `Foo` is the type constructor, and `Bar` and `Biz` are value constructors [Source 5](https://devtut.github.io/haskell/creating-custom-data-types.html).
To create a custom type class, you can use the `class` keyword followed by the type class name and type variable. Then, you define the type signatures of the functions associated with the type class. For example, to create a custom `Collection` type class, you can write:
```haskell
class Collection c e where
insert :: c -> e -> c
member :: c -> e -> Bool
```
Here, `Collection` is the type class name, and `c` and `e` are type variables. The `insert` and `member` functions have their type signatures defined [Source 10](https://en.wikibooks.org/wiki/Haskell/Advanced_type_classes#Multi-parameter_type_classes).
To create an instance of a custom type class for a specific type, use the `instance` keyword followed by the type class name and the specific type. Then, provide the function implementations associated with the type class. For example, to create an instance of the `Collection` type class for lists, you can write:
```haskell
instance Eq a => Collection [a] a where
insert = flip (:)
member = flip elem
```
Here, the `instance` keyword is followed by the `Collection` type class name and the list type `[a]`. The `insert` and `member` functions have their implementations provided [Source 10](https://en.wikibooks.org/wiki/Haskell/Advanced_type_classes#Multi-parameter_type_classes).
Haskell for Web Development?
Haskell is a versatile programming language that can be used for web development. There are several web frameworks available in Haskell that you can use to build web applications, such as Scotty, Servant, Yesod, IHP, and Spock [Source 1](https://www.freecodecamp.org/news/haskell-programming-language-introduction/), [Source 2](https://wiki.haskell.org/Web/Frameworks), [Source 4](https://www.yesodweb.com/), [Source 6](https://serokell.io/blog/ihp-interview), and [Source 10](https://www.reddit.com/r/haskell/comments/2wfap0/web_development_using_haskell/).
Yesod is a more complete web framework compared to others, and it is built for productive development of type-safe, RESTful, high-performance web applications [Source 1](https://www.freecodecamp.org/news/haskell-programming-language-introduction/), [Source 4](https://www.yesodweb.com/).
IHP (Integrated Haskell Platform) is a modern, batteries-included web framework that uses Haskell and Nix. It comes with everything you need to build web applications out of the box, and it provides a fully managed development environment [Source 6](https://serokell.io/blog/ihp-interview), [Source 7](https://ihp.digitallyinduced.com/).
Spock is another Haskell web framework for rapid development, offering a toolbox with routing, middleware, JSON, Blaze, sessions, cookies, database helper, and CSRF protection [Source 2](https://wiki.haskell.org/Web/Frameworks).
Haskell also has the `blaze-html` package used to build HTML files, similar to `gohtml` [Source 1](https://www.freecodecamp.org/news/haskell-programming-language-introduction/).
To connect your Haskell web application to a database, you can use libraries like HDBC for PostgreSQL [Source 5](https://stackoverflow.com/questions/1253340/what-is-the-ecosystem-for-haskell-web-development). For server connectivity and handling HTTP requests and responses, you can use libraries like WAI and Warp [Source 10](https://www.reddit.com/r/haskell/comments/2wfap0/web_development_using_haskell/).
In summary, Haskell offers a variety of web frameworks and libraries to build web applications. Depending on your requirements and preferences, you can choose a complete framework like Yesod or a more minimalistic approach using libraries like WAI and Warp.