Topic outline

  • Introduction

    Functional Programming is a very high form of programming. All programs are constructed using functions. They use parameters to pass values to each other. No variables are declared like in Procedural Programming. The following example calculates the square of a number in haskell.

    square :: Int -> Intsquare n = n * n

    In procedural langauges we use selection statements such as if and case to make decisions. Haskell can use ifs but the use of guards and pattern matching is more popular. The following example uses a pair of guards to determine which is the lower number:

    minNum :: Int -> Int -> IntminNum x y  | x <= y   = x  | otherwise = y

    This second example shows how pattern matching works in haskell.

    output :: Int -> Int -> Intoutput 0, y = youtput x, y = x

    When the function is called each alternative definition for the function is tried until the one fitting is found. Therefore if it is called with output 0,7 the first pattern will match and the second one will not be attempted. If output 3,7 is called then the first pattern will fail and the second will succeed.

  • Getting Started

    I recommend the Hugs compiler which you can get free from here. There are versions for most operating systems.

    Once install to start the Haskell interpreter

    • type hugs

    You can now load program files using the following command

    • :load filename.hs

    Once loaded you can run a program by calling its main function.

    Also check out GHC - The Glasgow Haskell Complier.

  • Square Numbers

    Squaring or multiplying two numbers together.

    Usage: square n where n is a number will return the square of the number.

    square :: Int -> Int
    square n = n * n

  • Minimum Numbers

    The following three functions implement a minimum number finder.

    Usage: min2 x y where x,y are whole numbers. Will return the lower number.

    min2:: Int -> Int -> Int
    min2 x y
      | x <= y = x
      | otherwise = y

    Alternate: min3 x y z

    min3:: Int -> Int -> Int -> Int
    min3 x y z
      | x <= y && x <= z   = x
      | y <= z             = y
      | otherwise          = z

    Or building on the previous example min4 x y z a.

    min4 :: Int -> Int -> Int -> Int -> Int
    min4 a b c d = min2 a (min3 b c d)

  • Lists

    This page demonstrates how lists in Haskell work and how they link to recursion. Tbe included programs demonstrate head and Tail Recursion in Haskell.

  • Fibonacci Numbers

    Fibonacci Numbers are a sequence of numbers such as 0, 1, 1, 2, 3, 5, 8, etc.

    This means for:

    • n0 = 0
    • n1 = 1
    • and all other numbers are a product of the previous two Fibonacci numbers.

    a full explanation is avaliable on wikipedia

    load: :load fibs.hs (Assuming you have started hugs). usage: fibs(n)

    fibs :: Int -> Int
    fibs n
        | n==0           =0
        | n==1           =1
        | otherwise      =sums(n-1) + sums(n-2)

    last tested on Hugs98

  • Factorials

    Assuming you have started Haskell using the hugs command you can compile the file with :load factorial.hs

    Usage: fac(5)

    fac n = if n == 0 then 1
    else n*fac( n - 1)

    last ran using Hugs98

  • Topic 7

  • Topic 8

  • Topic 9

  • Topic 10