Previous    Next

# Functional Programming Languages

func-tion: a mathematical correspondence that assigns exactly one element of one set to each element of the same or another set

Webster's Dictionary

## OVERVIEW

The mathematical notion of function is that if f(x) = a "this time", then f(x) = a "next time"; there is no other value equal to f(x). This allows the use of equational reasoning familiar from algebra: If a = f(x), then g(f(x), f(x)) is equivalent to g(a, a). Pure functional coding languages encourage a kind of coding in which equational reasoning works, as it does in mathematics. Imperative coding languages have similar syntax: af(x). But if we follow this by bf(x), there is no guarantee that a = b; the function f can have side effects on global variables that make it return a different value each time. Furthermore, a program might assign into variable x between calls to f(x), so f(x) really means a different thing each time. Higher-order functions Functional coding languages also allow functions to be passed as arguments to other functions, or returned as results. Functions that take functional arguments are called higher-order functions. Higher-order functions become particularly interesting if the language also supports nested functions with lexical scope (also called block structure). Lexical scope means that each function can refer to variables and parameters of any function in which it is nested. A higher-order functional language is one with nested scope and higher-order functions. What is the essence of functional programming? Is it equational reasoning or is it higher-order functions? There is no clear agreement about the answer to this question. In this chapter we will discuss three different flavors of "functional" language:

• FunJava The MiniJava language with higher-order functions. Because side effects are still permitted (and thus, equational reasoning won't work), this is an impure, higher-order functional language; other such languages are Scheme, ML, and Smalltalk.

• PureFunJava A language with higher-order functions and no side effects, capturing the essence of strict, pure functional languages (like the pure functional subset of ML).
• LazyFunJava A nonstrict, pure functional language that uses lazy evaluation like the language Haskell. Nonstrict pure functional languages support equational reasoning very well (see ).

A first-order, pure functional language such as SISAL supports equational reasoning but not higher-order functions.

 JaVa Previous    Next