BSD 4_4 release
[unix-history] / usr / src / old / lisp / fp / PSD.doc / manCh1.rno
CommitLineData
3edcb7c8
KB
1.\" Copyright (c) 1980 The Regents of the University of California.
2.\" All rights reserved.
c0a1d81a 3.\"
ad787160
C
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
3edcb7c8 19.\"
ad787160
C
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)manCh1.rno 6.2 (Berkeley) 4/17/91
c0a1d81a
NC
33.\"
34.sp
35.NS 1 "Background"
36.sp
37.pp
38\fBFP\fP stands for a \fIFunctional Programming\fP language.
39Functional programs
40deal with \fIfunctions\fP instead of \fIvalues\fP.
41There is no explicit representation of state, there are
42no assignment statments, and hence,
43no variables.
44Owing to the lack of state, FP functions are free from
45side-effects; so we
46say the FP is \fIapplicative\fP.
47.pp
48All functions take one argument and they are evaluated using
49the single FP
50operation, \fIapplication\fP (the colon ':' is the apply operator).
51For example, we read $~+^:^<3~4>~$ as \*(lqapply
52the function '+' to its argument <3 4>\*(rq.
53.pp
54Functional programs express a functional-level combination of
55their components
56instead of describing state changes using value-oriented
57expressions.
58For example, we write the function returning the
59\fIsin\fP of the \fIcos\fP
60of its input, \*(IE $sin(cos(x))$, as:
61$sin^@^cos$. This is a \fIfunctional expression\fP, consisting
62of the single \fIcombining form\fP called \fIcompose\fP ('@'
63is the compose operator)
64and its \fIfunctional arguments\fP \fIsin\fP and \fIcos\fP.
65.pp
66All combining forms take functions as arguments and return
67functions as results; functions may either be applied,
68\*(EG
69$sin @ cos^:^3$, or used as a functional argument in another functional
70expression, \*(EG \fItan @ sin @ cos\fP.
71.pp
72As we have seen,
73FP's combining forms allow us to express
74control abstractions without the use of variables.
75The \fIapply to all\fP functional form (&)
76is another case in point. The function '& exp'
77exponentiates all the elements of its argument:
78.sp 4p
79.EQ I (1.1)
80"&exp : <1.0 2.0>" ~==~ "<2.718 7.389>"
81.EN
82.sp 4p
83In (1.1)
84there are
85no induction variables, nor a
86loop bounds specification.
87Moreover, the code is useful for any size argument,
88so long as the sub-elements of its argument conform to the domain of the
89\fIexp\fP function.
90.pp
91We must change our view of the programming process
92to adapt to the functional
93style.
94Instead of writing down a set of steps that manipulate and assign values,
95we compose functional expressions
96using
97the higher-level functional forms.
98For example, the function that adds a
99scalar to all elements of a vector will be written in two steps. First,
100the function that distributes the scalar amongst each element
101of the vector:
102.sp 4p
103.EQ I (1.2)
104"distl : <3 <4 6>>" ~==~ "<<3 4> <3 6>>"
105.EN
106.sp 4p
107Next, the function that adds the pairs of elements that
108make up a sequence:
109.sp 4p
110.EQ I (1.3)
111"&+ : <<3 4> <3 6>>" ~==~ "<7 9>"
112.EN
113.sp 4p
114.pp
115In a value-oriented programming language the computation
116would be expressed as:
117.sp 4p
118.EQ I (1.4)
119"&+ : distl : <3 <4 6>>,"
120.EN
121.sp 4p
122which means to apply 'distl' to the input and then to apply '+'
123to every element of the result.
124In FP we write (1.4) as:
125.sp 4p
126.EQ I (1.5)
127"&+ @ distl : <3 <4 6>>."
128.EN
129.sp 4p
130The functional expression of (1.5) replaces
131the two step value expression of (1.4).
132.pp
133Often,
134functional expressions are built from the inside out,
135as in LISP.
136In the next example we derive a function that scales then
137shifts a vector, \*(IE for scalars $a,~b^$ and a vector $v vec$,
138compute $a~+~b v vec$. This FP function will have three
139arguments, namely $a,~b~$ and $~v vec$. Of course, FP
140does not use formal parameter names, so
141they will be designated by the function symbols 1, 2, 3.
142The first code segment scales $v vec~$ by $b$ (defintions
143are delimited with curly braces '{}'):
144.sp 4p
145.EQ I (1.6)
146"{scaleVec &\(** @ distl @ [2,3]}"
147.EN
148.sp 4p
149The code segment in (1.5)
150shifts the vector.
151The completed function is:
152.sp 4p
153.EQ I (1.7)
154"{changeVec &+ @ distl @ [1 , scaleVec]}"
155.EN
156.pp
157In the derivation of the program we wrote from right to left,
158first doing the \fIdistl\fP's and then composing with the
159\fIapply-to-all\fP functional form.
160Using an imperative language, such as Pascal,
161we would write the program from
162the outside in, writing the loop
163before inserting the arithmetic operators.
164.pp
165Although
166FP encourages a recursive programming style,
167it provides combining forms to avoid explicit recursion.
168For example, the
169right insert combining form (!)
170can be used to write a function
171that adds up a list of numbers:
172.sp 4p
173.EQ I (1.8)
174"!+ : <1 2 3>" ~==~ 6
175.EN
176.pp
177The equivalent, recursive function is much longer:
178.sp 4p
179.EQ I (1.9)
180"{addNumbers (null -> %0 ; + @ [1, addNumbers @ tl])}"
181.EN
182.pp
183The generality of the combining forms encourages hierarchical
184program development. Unlike APL,
185which restricts
186the use of combining forms to certain builtin functions,
187FP allows combining forms to take any functional expression as
188an argument.
189.bp