Skip to main content
Data Types Function Type Declartion Function Defintion Functional Principles Comments

Haskell Basics

Made by: CodingHome.
Published by: AT Products LLC.
Haskell Resources

Data Types

In Haskell, types are how you describe the data your program will work with. Types can be simple, like:

  • Bool: boolean True or False.
  • Char: one character.
  • Int: fixed-precision signed integer (usually 64-bit)
  • Float / Double: floating-point values.

Or, they can be composed with constructs, like:

  • String: shortcut for [Char].
  • [a]: list of as (where a is a general type).
  • (a, b): tuple composed of a and b
  • Just a: Maybe monad with the Just option

Function Type Declaration

Haskell's functions will usually have 2 parts: a type declaration, and the implementation of the function itself (the type part can also be inferred):

 
-- type declaration 
-- constraints => type -> ... -> returnType (the last type will always be the return type) 
countIns :: Eq a => a -> [a] -> Int
-- implementation 
countIns x = length . filter (== x)
  • Bool: boolean True or False.
  • Char: one character.
  • Int: fixed-precision signed integer (usually 64-bit)
  • Float / Double: floating-point values.

Notice the type declaration has 2 types of "arrows": => and ->.

  • => : used for giving additional context of what rules a should follow (in the example above, we see a should be part of the Eq class, which allows for comparison)
  • -> : used for actually declaring the type of the function (in the above example, we see the first argument is a, a general, comparable type, then a list of as and then finally it outputs an Integer)

Function Definition

Functions are defined similar to:

functionName argument(s) = expression

Haskell forces declarative style, so functions are (usually) composed of just an expression (always returns a value).

 
-- type declaration 
countIns :: Eq a => a -> [a] -> Int 
-- implementation 
-- function args = expression 
countIns x = length . filter (== x) 

Functional Principles

Haskell restricts to the functional paradigm, meaning Haskell enforces:

  • Immutability: when the data has to be defined again for it to "change"
  • Purity: functions must be pure, producing no side-effects (like modifying a global variable), and must be deterministic (f(x) = y, no matter when f is called)
  • Higher-order functions: functions must be first-class, meaning they can be passed as arguments to other functions. This allows for higher-order functions. which take a function and other data and apply the function to the data in different ways.
  • Disciplined state trying to minimize the use of mutable state and trying to make it local when possible.

Comments

In-line comments are made with -- and block comments with {- -}:


-- inline
commentHere = False {
- block commentHere = True -}