Haskell

GHCi

  • :reload reload all names in scope

  • :quit

  • :t 9 returns the type of 9

  • :info [] returns lots of useful details about the implemented typeclasses

  • :k Int returns the kind of Int

  • :set prompt "\x03BB > " would change the command prompt to λ >

  • :set -XExistentialQuantification sets up the language extension

You can give multiline inputarrow-up-right to GHCi as follows:

   *Main> :{
   *Main| let askname = do
   *Main|               putStrLn "What is your name?"
   *Main|               name <- getLine
   *Main|               putStrLn $ "Hello " ++ name
   *Main| :}
   *Main>

General syntax

Quick reference:

The best way to find out about these kind of constructs in ghci, e.g. :info <*>:

  • (.) - Function composition

  • <*>

  • $

Guard syntax

Guard Syntaxarrow-up-right allows us to conditionally check a statement within a pattern matching expression.

'where' clause

Define inline bindings (e.g. to functions or values). The following examplearrow-up-right uses guards and a where clase:

'as' Pattern

The 'as' pattern (described herearrow-up-right, herearrow-up-right and herearrow-up-right), gives us a way of matching a name for the entire element being pattern matched in a list:

The following would be read all as (x:xs):

Modules

This pagearrow-up-right from 'Learn you a Haskell' is a good reference.

  • Import hiding

Indentation

From this pagearrow-up-right, the golden rule of indentation is:

Code which is part of some expression should be indented further in than the beginning of that expression...All grouped expressions must be exactly aligned

But you can avoid indentation:

Indentation is actually optional if you instead use semicolons and curly braces

Types and Typeclasses

In the following snippet (referencearrow-up-right):

  • data means we're declaring a new type

  • Expr is the type constructor

  • I ..., Add ..., Mul ... are the value constructors

  • Adding deriving (Show) at the end of a data declaration automatically makes that type part of the Show typeclass (referencearrow-up-right):

Record syntax declarations

Syntactic sugararrow-up-right providing data accessors:

You can pattern match against them:

And create new versions with certain fields overwritten using:

newtype declarations

For the moment, I treat newtype as being broadly interchangable with data when creating a new type. There's documentation about it herearrow-up-right.

Typeclass: Semigroup

A set with an associative binary operation. Superset of Monoids.

Typeclass: Monoids

Monoidarrow-up-right is a typeclass with an associative mappend operation and an identity element.

Integer numbers form a monoid under addition with 0 as identity element Integer numbers form a monoid under multiplication with 1 as identity element Lists form a monoid under concatenation with the empty list as identity element

mappend is given the infix synonym (<>)

Generalised Algebraic Datatypes (GADTs)

The crux of GADTsarrow-up-right is that the provide a way to explicitly declare the type signature of each constructor.

The following language option is required:

And then an example would be:

Miscellaneous

Template Haskell

Template Haskellarrow-up-right is similar to Lisp macros.

It is used e.g. by lens to generate the various lenses into data structures at compile time

Forall Keyword

forall imposes constraints on parameterising typesarrow-up-right when defining new data types

Last updated