OSDN Git Service

699569d8811248feb6c53f5322fa4818ea588dc0
[ring-lang-081/ring.git] / docs / ja-jp / build / html / _sources / fp.txt
1 .. index:: 
2         single: 関数型プログラミング; はじめに
3
4 ====================
5 関数型プログラミング
6 ====================
7
8 以前の章では関数と再帰について学びました。
9
10 関数型プログラミング (FP) の概念についてより詳しく学びます。
11
12 * 純粋関数
13 * 第一級関数
14 * 高階関数
15 * 無名関数と入れ子関数
16 * 等式関数
17
18 .. index:: 
19         pair: 関数型プログラミング; 純粋関数
20
21
22 純粋関数
23 ========
24
25 代入演算子により、関数への参照で渡される元データを処理する代わりに、
26 変数の値 (リストとオブジェクト)をコピーして新しい変数を作成することで
27 純粋関数 (状態を変更しない関数) を作成できます。
28
29
30 用例:
31
32 .. code-block:: ring
33
34         Func Main
35                 aList = [1,2,3,4,5]
36                 aList2 = square(aList)
37                 see "aList" + nl
38                 see aList
39                 see "aList2" + nl
40                 see aList2
41
42         Func Square aPara
43                 a1 = aPara              # リストのコピー
44                 for x in a1
45                         x *= x
46                 next
47                 return a1               # 新しいリストを返します。
48                 
49 実行結果:
50
51 .. code-block:: ring
52
53         aList
54         1
55         2
56         3
57         4
58         5
59         aList2
60         1
61         4
62         9
63         16
64         25
65
66
67 .. index:: 
68         pair: 関数型プログラミング; 第一級関数
69
70 第一級関数
71 ==========
72
73 Ring の関数は第一級オブジェクトです。関数を仮引数として渡したり、
74 変数として格納、または値を返せます。
75
76 例えば、関数名を "関数名" または :関数名 のようにリテラルとして記述することで、関数の渡したり、返せます。
77
78 関数名を有する変数を用いると、関数を渡したり返せます。
79
80 Call 命令で関数名を有する変数から関数を呼び出します。
81
82 文法:
83
84 .. code-block:: ring
85
86         [関数の返値を代入するための変数] = Call 変数名([仮引数])
87
88 用例:
89
90 .. code-block:: ring
91
92         Func Main
93                 see "before test2()" + nl
94                 f = Test2(:Test)
95                 see "after test2()" + nl
96                 call f()
97
98         Func Test
99                 see "Message from test!" + nl
100
101         Func Test2 f1
102                 call f1()
103                 See "Message from test2!" + nl
104                 return f1       
105
106 実行結果:
107
108 .. code-block:: ring
109
110         before test2()
111         Message from test!
112         Message from test2!
113         after test2()
114         Message from test!
115
116 .. index:: 
117         pair: 関数型プログラミング; 高階関数
118
119 高階関数
120 ========
121
122 高階関数は、ほかの関数を仮引数として扱う関数です。
123
124 用例:
125
126 .. code-block:: ring
127
128         Func Main
129                 times(5,:test)
130
131         Func Test
132                 see "Message from the test function!" + nl
133
134         Func Times nCount,F
135
136                 for x = 1 to nCount
137                         Call F()
138                 next    
139
140 実行結果:
141
142 .. code-block:: ring
143
144         Message from the test function!
145         Message from the test function!
146         Message from the test function!
147         Message from the test function!
148         Message from the test function!
149
150 .. index:: 
151         pair: 関数型プログラミング; 無名関数と入れ子関数
152
153 無名関数と入れ子関数
154 ====================
155
156 無名関数は、名前を持たない関数であり、ほかの関数へ仮引数として渡したり、変数へ格納できます。
157
158 文法:
159
160 .. code-block:: ring
161
162         Func [仮引数] { [ステートメント] }
163
164 用例:
165
166 .. code-block:: ring
167
168         test( func x,y { 
169                                 see "hello" + nl
170                                 see "Sum : " + (x+y) + nl
171                        } )
172
173         new great { f1() }
174
175         times(3, func { see "hello world" + nl } )
176
177         func test x
178                 call x(3,3)
179                 see "wow!" + nl
180
181         func times n,x
182                 for t=1 to n
183                         call x()
184                 next
185
186         Class great
187                 func f1
188                         f2( func { see "Message from f1" + nl } )
189
190                 func f2 x
191                         call x()
192
193 実行結果:
194
195 .. code-block:: ring
196
197         hello
198         Sum : 6
199         wow!
200         Message from f1
201         hello world
202         hello world
203         hello world
204
205 用例:
206
207 .. code-block:: ring
208
209         Func Main
210                 aList = [1,2,3,4]
211                 Map (aList , func x { 
212                                         return x*x 
213                                     } )
214                 see aList
215                 aList = [4,9,14,25]
216                 Map(aList, :myfilter )
217                 see aList
218                 aList = [11,12,13,14]
219                 Map (aList , func x {
220                         if x%2=0
221                                 return "even"
222                         else
223                                 return "odd"
224                         ok
225                 })
226                 see aList
227
228         Func myfilter x
229                 if x = 9
230                         return "True"
231                 else
232                         return "False"
233                 ok
234
235         Func Map aList,cFunc
236                 for x in aList
237                         x = call cFunc(x)
238                 next
239
240 実行結果:
241
242 .. code-block:: ring
243
244         1
245         4
246         9
247         16
248         False
249         True
250         False
251         False
252         odd
253         even
254         odd
255         even
256
257 .. index:: 
258         pair: 関数型プログラミング; 等式関数
259
260 等式関数
261 ========
262
263 if 関数 = 関数、または演算子‘=’や‘!=’を用いずにテストできます (この機能は関数の等価性、あるいは Equality of functions とも言います)。
264
265 用例:
266
267 .. code-block:: ring
268
269         f1 = func { see "hello" + nl }
270
271         f2 = func { see "how are you?" + nl }
272
273         f3 = f1
274
275         call f1()
276         call f2()
277         call f3()
278
279         see (f1 = f2) + nl
280         see (f2 = f3) + nl
281         see (f1 = f3) + nl
282
283 実行結果:
284
285 .. code-block:: ring
286
287         hello
288         how are you?
289         hello
290         0
291         0
292         1
293
294 .. index:: 
295         pair: 関数型プログラミング; 参考文献
296
297 参考文献
298 ========
299
300 * How is the equality of functions defined? - Quora : https://www.quora.com/How-is-the-equality-of-functions-defined