CPS506 - Comparative Programming Languages - Winter 2022

Lab 7 - A Brief Introduction to Haskell

Objectives

  • Gain an introduction to the functional program paradigm
  • Build an understanding of basic Haskell
  • Work with lists and write simple functions to manipulate them

Introduction

Functional programming languages represent a radical departure from the procedural or object-oriented paradigms that most programmers are familar with. These languages have their origin in the lambda calculus, a mathematical system for expressing computation as the application of functions invented by Alonzo Church in the 1930s. Functions in functional programming languages have the form of those in mathematics, mapping a set of inputs in one domain to a set of outputs in another.

Haskell is even more pure than Elixir, since even I/O can only be handled in a very stylized manner. The most significant difference between Elixir and Haskell is that Haskell is statically typed.

Installing

These labs use the Glasgow Haskell Compiler (ghc). If you wish to work on the Haskell labs at home, you will need the platform found here.

Learn you a Haskell

Learn you a Haskell is a free and informative resource on learning to program with Haskell. Work your way through the first five chapters before attempting the rest of this lab (they are brief).

Working with lists

Write and submit a Haskell script with the following functions:

  • thirdLast:: [a] -> a
    • Returns the third last item in the input list
  • fibN:: Num -> Num
    • Given a number n, returns the nth Fibonacci number
  • roots:: Float -> Float -> Float -> (Float, Float)
    • Given the a, b and c of a quadratic equation of the form ax^2+bx+c, it returns a pair containing the roots

Make sure your script has a .hs extension and put it in your CPS506 Fossil repository.