# Class 04: Introduction to Functional Programming

Back to Design Issues. On to Untyped Functional Programming in Scheme.

Held Monday, February 1

Summary

• The functional model
• Variations on functional programming
• History of functional programming, revisited

Contents

Notes

• There seem to be some problems with the MathLAN server. Remember that you can always access pages within the MathLAN at
`file:///home/rebelsky/public_html/Courses/CS152/99S/`
• You should be able to figure out the other links from there, too.
• Are there questions on the readings?
• Are there questions on the first assignment?

## Functional Languages

• The language concepts we've looked at so far primarly relate to imperative languages, which emphasize basic operations and control.
• As we know, there are a number of other important paradigms for programming. One of the most important is the functional paradigm, in which programs are collections of function definitions and function applications.

### Defining Functions

• As always, we should try to define our basis of study.
• What is a function?
• A rule that associates members of one set (the domain) with members of another set (the range).
• The domain and range may be the same.
• Each member of the domain is associated with at most one member of the range.
• A (potentially infinite) set of domain/range pairs.
• What are partial functions?
• How can/should/do we define functions?
• By listing all the pairs.
• By providing some sort of general form for pairs (e.g., regular expressions?)
• In terms of already-defined functions.
• By writing "computer programs" that generate results.
• How do functions on the computer differ from "mathematical" functions?
• In math, we assume that when we apply the same function to the same value, we get the same result. We don't always make this assumption in computer languages. For example, `read()` might be expected to return a different byte each time.
• In math, we often assume that domains and ranges are infinite.
• ...
• What kinds of values can functions return?
• Can they return other functions?

### Pure Functional Input and Output (I)

• Are `read` and `write` functions, in the mathematical sense? Why or why not?
• How about `random`?
• How might we make `random` a pure function?
• How about `read` and `write` pure functions?

### Attributes of Functional Languages

• When people use the term "functional language", they often mean more than just the ability to define and apply functions. Definitions of "functional" often include
• Functions as basic values. That is, you can write functions that take other functions as arguments or return functions. (This is core to most understandings of functional languages.)
• ``Atomic symbols'' as basic values (so that you can work with symbols in addition to strings, integers, and such).
• Dynamic lists as built-in data structures.
• Different treatment of memory and variables?

### History

As always, this is biased and only partially correct.

• The study of languages describing functions began with Alonzo Church's work on the Lambda Calculus in the 1920's.
• Church's purpose was to consider how one should describe functions in the abstract.
• LISP (late 1950's) was the first functional programming language.
• While LISP includes lambdas, I believe McCarthy has indicated that he did not quite understand their implications.
• Most early computer languages included functions. However, there were clear limitations on what functions could and could not do.
• Fortran did not support natural recursive functions (no stack).
• Algol had some interesting mechanisms for handling arguments.
• Unlike many languages, which use static scoping (the meaning of a variable can be determined by a static analysis of the program), LISP supported dynamic scoping. This made LISP hard to describe formally. A number of LISP variants appeared, using different scoping.
• Common LISP
• Franz LISP
• T
• Scheme
• LISP and Scheme include impure components to better support traditional notions of programming.
• FP was defined by John Backus in a Turing-award lecture in the late 1970's to illustrate some of the possibilities of programming with functions and no notion of an underlying architecture. FP provides no assignment or direct access to memory.
• ML (late 1970's?) is a popular functional language originally developed as part of a program verification system. It is currently known for its use of modern language concepts (an excellent type system, exceptions, polymorphism, etc.)
• Miranda (early 1980's?) was one of the first pure lazy functional programming languages. (We'll come back to laziness later.)
• Haskell (early 1990's) is one of the newest ``big'' functional languages. Haskell is notable for making no compromises on functional purity -- there are no side-effecting operations in Haskell. Haskell also uses an intersting evaluation strategy called lazy evaluation.

History

• Created Tuesday, January 19, 1999 as a blank outline.
• Filled in the details on Monday, February 1, 1999. Relied, in part, on outline 20 and on outline 21 from last year's class.
• Removed information on LISP on Wednesday, February 3.

Back to Design Issues. On to Untyped Functional Programming in Scheme.

Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.