xref: /original-bsd/old/lisp/fp/PSD.doc/manCh1.rno (revision 4e9331e4)
Copyright (c) 1980 Regents of the University of California.
All rights reserved. The Berkeley software License Agreement
specifies the terms and conditions for redistribution.

@(#)manCh1.rno 6.1 (Berkeley) 04/29/86

.NS 1 "Background" .pp FP stands for a Functional Programming language. Functional programs deal with functions instead of values. There is no explicit representation of state, there are no assignment statments, and hence, no variables. Owing to the lack of state, FP functions are free from side-effects; so we say the FP is applicative. .pp All functions take one argument and they are evaluated using the single FP operation, application (the colon ':' is the apply operator). For example, we read $~+^:^<3~4>~$ as \*(lqapply the function '+' to its argument <3 4>\*(rq. .pp Functional programs express a functional-level combination of their components instead of describing state changes using value-oriented expressions. For example, we write the function returning the sin of the cos of its input, \*(IE $sin(cos(x))$, as: $sin^@^cos$. This is a functional expression, consisting of the single combining form called compose ('@' is the compose operator) and its functional arguments sin and cos. .pp All combining forms take functions as arguments and return functions as results; functions may either be applied, \*(EG $sin @ cos^:^3$, or used as a functional argument in another functional expression, \*(EG tan @ sin @ cos. .pp As we have seen, FP's combining forms allow us to express control abstractions without the use of variables. The apply to all functional form (&) is another case in point. The function '& exp' exponentiates all the elements of its argument: "&exp : <1.0 2.0>" ~==~ "<2.718 7.389>" .EN In (1.1) there are no induction variables, nor a loop bounds specification. Moreover, the code is useful for any size argument, so long as the sub-elements of its argument conform to the domain of the exp function. .pp We must change our view of the programming process to adapt to the functional style. Instead of writing down a set of steps that manipulate and assign values, we compose functional expressions using the higher-level functional forms. For example, the function that adds a scalar to all elements of a vector will be written in two steps. First, the function that distributes the scalar amongst each element of the vector: "distl : <3 <4 6>>" ~==~ "<<3 4> <3 6>>" .EN Next, the function that adds the pairs of elements that make up a sequence: "&+ : <<3 4> <3 6>>" ~==~ "<7 9>" .EN .pp In a value-oriented programming language the computation would be expressed as: "&+ : distl : <3 <4 6>>," .EN which means to apply 'distl' to the input and then to apply '+' to every element of the result. In FP we write (1.4) as: "&+ @ distl : <3 <4 6>>." .EN The functional expression of (1.5) replaces the two step value expression of (1.4). .pp Often, functional expressions are built from the inside out, as in LISP. In the next example we derive a function that scales then shifts a vector, \*(IE for scalars $a,~b^$ and a vector $v vec$, compute $a~+~b v vec$. This FP function will have three arguments, namely $a,~b~$ and $~v vec$. Of course, FP does not use formal parameter names, so they will be designated by the function symbols 1, 2, 3. The first code segment scales $v vec~$ by $b$ (defintions are delimited with curly braces '{}'): "{scaleVec &\(** @ distl @ [2,3]}" .EN The code segment in (1.5) shifts the vector. The completed function is: "{changeVec &+ @ distl @ [1 , scaleVec]}" .EN .pp In the derivation of the program we wrote from right to left, first doing the distl's and then composing with the apply-to-all functional form. Using an imperative language, such as Pascal, we would write the program from the outside in, writing the loop before inserting the arithmetic operators. .pp Although FP encourages a recursive programming style, it provides combining forms to avoid explicit recursion. For example, the right insert combining form (!) can be used to write a function that adds up a list of numbers: "!+ : <1 2 3>" ~==~ 6 .EN .pp The equivalent, recursive function is much longer: "{addNumbers (null -> %0 ; + @ [1, addNumbers @ tl])}" .EN .pp The generality of the combining forms encourages hierarchical program development. Unlike APL, which restricts the use of combining forms to certain builtin functions, FP allows combining forms to take any functional expression as an argument. .bp