Lecture 01 Introduction

Joseph Haugh

University of New Mexico

Contact Information

  • Instructor: Joseph Haugh
  • Email: jhaugh@cs.unm.edu
  • Office: FEC 2170

Textbooks

  • Programming in Haskell Second Edition by Graham Hutton

Grading

  • 60% exams
  • 30% flexible assignments
  • 10% project

Late Policy

  • Assignments will automatically be docked 10% per day they are late after the assigned due date.

Course Description

  • Can you imagine programming without assignment?
  • Can you imagine programming without loops?
  • How about with no mutation of any kind?
  • This class will force you to confront these questions and hopefully come out a better programmer because of it.

What is Declarative Programming?

  • Describing what a program should do rather than how it should do it
  • Referential transparency
  • Close relationship to math

What is Referential Transparency?

  • An expression that can be replaced with its value and vice versa without changing the behavior of the program.
  • Put another way, a function is referentially transparent if for a given input it returns the same output every time, i.e. a lookup table.
int x = 0;
int foo(int y) {
    return y * 2;
}
int bar(int y) {
    return x++ * y;
}
int main() {
    printf("x = %d\n", x);
    int z = 2;
    int e1 = foo(z) + z - (foo(z) - foo(z));
    printf("x = %d, e1 = %d\n", x, e1);
    int e2 = foo(z) + z - (bar(z) - bar(z));
    printf("x = %d, e2 = %d\n", x, e2);
    return 0;
}

What is functional programming?

  • Sub category under declarative programming
  • Focuses on functions
  • First class functions:
    • Functions are values in the language
    • This means they can be passed as arguments and returned as values from functions
  • Composing functions:
    • Combining two functions into one function
  • Pure (referentially transparent) functions
  • Anonymous functions (lambdas):
    • Function without a name
    • Has become a more common part of programmers vernacular after being added to most modern languages

Origins of functional programming

  • Are the ideas of functional programming new?
  • Which language was the first to feature these ideas?
  • LISP! (LISt Processing)
  • What year did Lisp come out?
  • 1958! Making it the second oldest high level programming language only being beat out by FORTRAN (FORmula TRANslating system)
  • Was invented by John McCarthy

Haskell

  • Lazy
  • Automatically curried
  • Has pattern matching
  • Statically typed
  • Immutable*
  • Free of loops*
  • Free of side effects*

Language Shock

  • No loops you say?? No MUTATION?? NO SIDE EFFECTS???? How do you get anything done??
  • I encourage you to embrace these changes and see what you can discover while programming in these languages.
  • I want to leave you with this quote from Orson Welles: “The enemy of art is the absence of limitations.”