Commit | Line | Data |
---|---|---|
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. | |
39 | Functional programs | |
40 | deal with \fIfunctions\fP instead of \fIvalues\fP. | |
41 | There is no explicit representation of state, there are | |
42 | no assignment statments, and hence, | |
43 | no variables. | |
44 | Owing to the lack of state, FP functions are free from | |
45 | side-effects; so we | |
46 | say the FP is \fIapplicative\fP. | |
47 | .pp | |
48 | All functions take one argument and they are evaluated using | |
49 | the single FP | |
50 | operation, \fIapplication\fP (the colon ':' is the apply operator). | |
51 | For example, we read $~+^:^<3~4>~$ as \*(lqapply | |
52 | the function '+' to its argument <3 4>\*(rq. | |
53 | .pp | |
54 | Functional programs express a functional-level combination of | |
55 | their components | |
56 | instead of describing state changes using value-oriented | |
57 | expressions. | |
58 | For example, we write the function returning the | |
59 | \fIsin\fP of the \fIcos\fP | |
60 | of its input, \*(IE $sin(cos(x))$, as: | |
61 | $sin^@^cos$. This is a \fIfunctional expression\fP, consisting | |
62 | of the single \fIcombining form\fP called \fIcompose\fP ('@' | |
63 | is the compose operator) | |
64 | and its \fIfunctional arguments\fP \fIsin\fP and \fIcos\fP. | |
65 | .pp | |
66 | All combining forms take functions as arguments and return | |
67 | functions as results; functions may either be applied, | |
68 | \*(EG | |
69 | $sin @ cos^:^3$, or used as a functional argument in another functional | |
70 | expression, \*(EG \fItan @ sin @ cos\fP. | |
71 | .pp | |
72 | As we have seen, | |
73 | FP's combining forms allow us to express | |
74 | control abstractions without the use of variables. | |
75 | The \fIapply to all\fP functional form (&) | |
76 | is another case in point. The function '& exp' | |
77 | exponentiates 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 | |
83 | In (1.1) | |
84 | there are | |
85 | no induction variables, nor a | |
86 | loop bounds specification. | |
87 | Moreover, the code is useful for any size argument, | |
88 | so long as the sub-elements of its argument conform to the domain of the | |
89 | \fIexp\fP function. | |
90 | .pp | |
91 | We must change our view of the programming process | |
92 | to adapt to the functional | |
93 | style. | |
94 | Instead of writing down a set of steps that manipulate and assign values, | |
95 | we compose functional expressions | |
96 | using | |
97 | the higher-level functional forms. | |
98 | For example, the function that adds a | |
99 | scalar to all elements of a vector will be written in two steps. First, | |
100 | the function that distributes the scalar amongst each element | |
101 | of the vector: | |
102 | .sp 4p | |
103 | .EQ I (1.2) | |
104 | "distl : <3 <4 6>>" ~==~ "<<3 4> <3 6>>" | |
105 | .EN | |
106 | .sp 4p | |
107 | Next, the function that adds the pairs of elements that | |
108 | make 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 | |
115 | In a value-oriented programming language the computation | |
116 | would be expressed as: | |
117 | .sp 4p | |
118 | .EQ I (1.4) | |
119 | "&+ : distl : <3 <4 6>>," | |
120 | .EN | |
121 | .sp 4p | |
122 | which means to apply 'distl' to the input and then to apply '+' | |
123 | to every element of the result. | |
124 | In FP we write (1.4) as: | |
125 | .sp 4p | |
126 | .EQ I (1.5) | |
127 | "&+ @ distl : <3 <4 6>>." | |
128 | .EN | |
129 | .sp 4p | |
130 | The functional expression of (1.5) replaces | |
131 | the two step value expression of (1.4). | |
132 | .pp | |
133 | Often, | |
134 | functional expressions are built from the inside out, | |
135 | as in LISP. | |
136 | In the next example we derive a function that scales then | |
137 | shifts a vector, \*(IE for scalars $a,~b^$ and a vector $v vec$, | |
138 | compute $a~+~b v vec$. This FP function will have three | |
139 | arguments, namely $a,~b~$ and $~v vec$. Of course, FP | |
140 | does not use formal parameter names, so | |
141 | they will be designated by the function symbols 1, 2, 3. | |
142 | The first code segment scales $v vec~$ by $b$ (defintions | |
143 | are delimited with curly braces '{}'): | |
144 | .sp 4p | |
145 | .EQ I (1.6) | |
146 | "{scaleVec &\(** @ distl @ [2,3]}" | |
147 | .EN | |
148 | .sp 4p | |
149 | The code segment in (1.5) | |
150 | shifts the vector. | |
151 | The completed function is: | |
152 | .sp 4p | |
153 | .EQ I (1.7) | |
154 | "{changeVec &+ @ distl @ [1 , scaleVec]}" | |
155 | .EN | |
156 | .pp | |
157 | In the derivation of the program we wrote from right to left, | |
158 | first doing the \fIdistl\fP's and then composing with the | |
159 | \fIapply-to-all\fP functional form. | |
160 | Using an imperative language, such as Pascal, | |
161 | we would write the program from | |
162 | the outside in, writing the loop | |
163 | before inserting the arithmetic operators. | |
164 | .pp | |
165 | Although | |
166 | FP encourages a recursive programming style, | |
167 | it provides combining forms to avoid explicit recursion. | |
168 | For example, the | |
169 | right insert combining form (!) | |
170 | can be used to write a function | |
171 | that adds up a list of numbers: | |
172 | .sp 4p | |
173 | .EQ I (1.8) | |
174 | "!+ : <1 2 3>" ~==~ 6 | |
175 | .EN | |
176 | .pp | |
177 | The 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 | |
183 | The generality of the combining forms encourages hierarchical | |
184 | program development. Unlike APL, | |
185 | which restricts | |
186 | the use of combining forms to certain builtin functions, | |
187 | FP allows combining forms to take any functional expression as | |
188 | an argument. | |
189 | .bp |