-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosettacode-examples.txt
476 lines (361 loc) · 14.1 KB
/
rosettacode-examples.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
--------------------
99_Bottles_of_Beer
--------------------
=={{header|FunL}}==
<lang funl>val
numbers = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six', 7:'seven',
8:'eight', 9:'nine', 10:'ten', 11:'eleven', 12:'twelve'}
alt = {3:'thir', 5:'fif'}
def
suffix( a, b ) = (if a.endsWith( 't' ) then a.substring( 0, a.length()-1 ) else a) + b
number( n@(13 | 15) ) = suffix( alt(n%10), 'teen' )
number( 20 ) = 'twenty'
number( n@(30 | 50) ) = suffix( alt(n\10), 'ty' )
number( n )
| n <= 12 = numbers(n)
| n <= 19 = suffix( numbers(n%10), 'teen' )
| 10|n = suffix( numbers(n\10), 'ty' )
| otherwise = number( n\10*10 ) + '-' + number( n%10 )
cap( s ) = s.substring( 0, 1 ).toUpperCase() + s.substring( 1, s.length() )
bottles( 0 ) = 'no more bottles'
bottles( 1 ) = 'one bottle'
bottles( n ) = number( n ) + ' bottles'
verse( 0 ) = ('No more bottles of beer on the wall, no more bottles of beer.\n'
+ 'Go to the store and buy some more, ninety-nine bottles of beer on the wall.')
verse( n ) = (cap( bottles(n) ) + ' of beer on the wall, ' + bottles( n ) + ' of beer.\n'
+ 'Take one down and pass it around, ' + bottles( n-1 )
+ ' of beer on the wall.\n')
for i <- 99..0 by -1 do println( verse(i) )</lang>
--------------------
A%2BB
--------------------
=={{header|FunL}}==
<lang funl>println( sum(map(int, readLine().split(' +'))) )</lang>
--------------------
Catalan_numbers
--------------------
=={{header|FunL}}==
<lang funl>import integers.choose
import util.TextTable
def
catalan( n ) = choose( 2n, n )/(n + 1)
catalan2( n ) = product( (n + k)/k | k <- 2..n )
catalan3( 0 ) = 1
catalan3( n ) = 2*(2n - 1)/(n + 1)*catalan3( n - 1 )
t = TextTable()
t.header( 'n', 'definition', 'product', 'recursive' )
t.line()
for i <- 1..4
t.rightAlignment( i )
for i <- 0..15
t.row( i, catalan(i), catalan2(i), catalan3(i) )
println( t )</lang>
{{out}}
<pre>
+----+------------+---------+-----------+
| n | definition | product | recursive |
+----+------------+---------+-----------+
| 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 2 |
| 3 | 5 | 5 | 5 |
| 4 | 14 | 14 | 14 |
| 5 | 42 | 42 | 42 |
| 6 | 132 | 132 | 132 |
| 7 | 429 | 429 | 429 |
| 8 | 1430 | 1430 | 1430 |
| 9 | 4862 | 4862 | 4862 |
| 10 | 16796 | 16796 | 16796 |
| 11 | 58786 | 58786 | 58786 |
| 12 | 208012 | 208012 | 208012 |
| 13 | 742900 | 742900 | 742900 |
| 14 | 2674440 | 2674440 | 2674440 |
| 15 | 9694845 | 9694845 | 9694845 |
+----+------------+---------+-----------+
</pre>
--------------------
Command-line_arguments
--------------------
=={{header|FunL}}==
<lang funl>println( args )</lang>
--------------------
Dot_product
--------------------
=={{header|FunL}}==
<lang funl>import lists.zipWith
def dot( a, b )
| a.length() == b.length() = sum( zipWith((*), a, b) )
| otherwise = error( "Vector sizes must match" )
println( dot([1, 3, -5], [4, -2, -1]) )</lang>
{{out}}<pre>3</pre>
--------------------
Execute_a_system_command
--------------------
=={{header|FunL}}==
<lang funl>import sys.execute
execute( if $os.startsWith('Windows') then 'dir' else 'ls' )</lang>
--------------------
Fibonacci_sequence
--------------------
=={{header|FunL}}==
=== Recursive ===
<lang funl>def
fib( 0 ) = 0
fib( 1 ) = 1
fib( n ) = fib( n - 1 ) + fib( n - 2 )</lang>
=== Tail Recursive ===
<lang funl>def fib( n ) =
def
_fib( 0, prev, _ ) = prev
_fib( 1, _, next ) = next
_fib( n, prev, next ) = _fib( n - 1, next, next + prev )
_fib( n, 0, 1 )</lang>
=== Lazy List ===
<lang funl>val fib =
def _fib( a, b ) = a # _fib( b, a + b )
_fib( 0, 1 )
println( fib(10000) )</lang>
{{out}}
<pre>
3364476487643178326662161200510754331030214846068006390656476997468008144216666236815559551363373402558206533268083615937373479048386526826304089246305643188735454436955982749160660209988418393386465273130008883026923567361313511757929743785441375213052050434770160226475831890652789085515436615958298727968298751063120057542878345321551510387081829896979161312785626503319548714021428753269818796204693609787990035096230229102636813149319527563022783762844154036058440257211433496118002309120828704608892396232883546150577658327125254609359112820392528539343462090424524892940390170623388899108584106518317336043747073790855263176432573399371287193758774689747992630583706574283016163740896917842637862421283525811282051637029808933209990570792006436742620238978311147005407499845925036063356093388383192338678305613643535189213327973290813373264265263398976392272340788292817795358057099369104917547080893184105614632233821746563732124822638309210329770164805472624384237486241145309381220656491403275108664339451751216152
6545361333111314042436854805106765843493523836959653428071768775328348234345557366719731392746273629108210679280784718035329131176778924659089938635459327894523777674406192240337638674004021330343297496902028328145933418826817683893072003634795623117103101291953169794607632737589253530772552375943788434504067715555779056450443016640119462580972216729758615026968443146952034614932291105970676243268515992834709891284706740862008587135016260312071903172086094081298321581077282076353186624611278245537208532365305775956430072517744315051539600905168603220349163222640885248852433158051534849622434848299380905070483482449327453732624567755879089187190803662058009594743150052402532709746995318770724376825907419939632265984147498193609285223945039707165443156421328157688908058783183404917434556270520223564846495196112460268313970975069382648706613264507665074611512677522748621598642530711298441182622661057163515069260029861704945425047491378115154139941550671256271197133252763631939606902895650288268608362241082050562
430701794976171121233066073310059947366875
</pre>
=== Iterative ===
<lang funl>def fib( n ) =
a, b = 0, 1
for i <- 1..n
a, b = b, a+b
a</lang>
=== Binet's Formula ===
<lang funl>import math.sqrt
def fib( n ) =
phi = (1 + sqrt( 5 ))/2
int( (phi^n - (-phi)^-n)/sqrt(5) + .5 )</lang>
=== Matrix Exponentiation ===
<lang funl>def mul( a, b ) =
res = array( a.length(), b(0).length() )
for i <- 0:a.length(), j <- 0:b(0).length()
res( i, j ) = sum( a(i, k)*b(k, j) | k <- 0:b.length() )
vector( res )
def
pow( _, 0 ) = ((1, 0), (0, 1))
pow( x, 1 ) = x
pow( x, n )
| 2|n = pow( mul(x, x), n\2 )
| otherwise = mul(x, pow( mul(x, x), (n - 1)\2 ) )
def fib( n ) = pow( ((0, 1), (1, 1)), n )(0, 1)
for i <- 0..10
println( fib(i) )</lang>
{{out}}
<pre>
0
1
1
2
3
5
8
13
21
34
55
</pre>
--------------------
Greatest_common_divisor
--------------------
=={{header|FunL}}==
FunL has pre-defined function <code>gcd</code> in module <code>integers</code> defined as:
<lang funl>def
gcd( 0, 0 ) = error( 'integers.gcd: gcd( 0, 0 ) is undefined' )
gcd( a, b ) =
def
_gcd( a, 0 ) = a
_gcd( a, b ) = _gcd( b, a%b )
_gcd( abs(a), abs(b) )</lang>
--------------------
Greatest_element_of_a_list
--------------------
=={{header|FunL}}==
Using the pre-defined function <code>max</code>:
<lang funl>println( max([1,2,3,-1,0]) )</lang>
{{out}}
<pre>
3
</pre>
It can be implemented as:
<lang funl>def
maximum( xs ) =
def
max( a, b ) = if a <= b then b else a
foldl( f, z, [] ) = z
foldl( f, z, x:xs ) = foldl( f, f(z, x), xs )
foldl1( f, x:xs ) = foldl( f, x, xs )
foldl1( _, [] ) = error( "foldl1: empty list" )
foldl1( max, xs )</lang>
--------------------
Hailstone_sequence
--------------------
=={{header|FunL}}==
<lang funl>def
hailstone( 1 ) = [1]
hailstone( n ) = n # hailstone( if 2|n then n/2 else n*3 + 1 )
if _name_ == '-main-'
h27 = hailstone( 27 )
assert( h27.length() == 112 and h27.startsWith([27, 82, 41, 124]) and h27.endsWith([8, 4, 2, 1]) )
val (n, len) = maxBy( snd, (i, hailstone( i ).length()) | i <- 1:100000 )
println( n, len )</lang>
{{out}}
<pre>
77031, 351
</pre>
--------------------
Haversine_formula
--------------------
=={{header|FunL}}==
<lang funl>import math.*
def haversin( theta ) = (1 - cos( theta ))/2
def radians( deg ) = deg Pi/180
def haversine( (lat1, lon1), (lat2, lon2) ) =
R = 6372.8
h = haversin( radians(lat2 - lat1) ) + cos( radians(lat1) ) cos( radians(lat2) ) haversin( radians(lon2 - lon1) )
2R asin( sqrt(h) )
println( haversine((36.12, -86.67), (33.94, -118.40)) )</lang>
{{out}}
<pre>
2887.259950607111
</pre>
--------------------
Hello_world/Graphical
--------------------
=={{header|FunL}}==
<lang funl>native javax.swing.{SwingUtilities, JPanel, JLabel, JFrame}
native java.awt.Font
def createAndShowGUI( msg ) =
f = JFrame()
f.setTitle( msg )
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
p = JPanel()
l = JLabel( msg )
l.setFont( Font.decode(Font.SERIF + ' 150') )
p.add( l )
f.add( p )
f.pack()
f.setResizable( false )
f.setVisible( true )
SwingUtilities.invokeLater( createAndShowGUI.runnable('Goodbye, World!') )</lang>
--------------------
Hello_world/Web_server
--------------------
=={{header|FunL}}==
<lang funl>native java.io.PrintWriter
native java.net.ServerSocket
val port = 8080
val listener = ServerSocket( port )
printf( 'Listening at port %1$d\n', port )
forever
socket = listener.accept()
PrintWriter( socket.getOutputStream(), true ).println( 'hello world' )
socket.shutdownOutput()
socket.close()</lang>
--------------------
Integer_sequence
--------------------
=={{header|FunL}}==
The following has no limit since FunL has arbitrary size integers.
<lang funl>for i <- 1.. do println( i )</lang>
--------------------
Least_common_multiple
--------------------
=={{header|FunL}}==
FunL has function <code>lcm</code> in module <code>integers</code> with the following definition:
<lang funl>def
lcm( _, 0 ) = 0
lcm( 0, _ ) = 0
lcm( x, y ) = abs( (x\gcd(x, y)) y )</lang>
--------------------
Narcissistic_decimal_number
--------------------
=={{header|FunL}}==
<lang funl>def narcissistic( start ) =
power = 1
powers = array( 0..9 )
def narc( n ) =
num = n.toString()
m = num.length()
if power != m
power = m
powers( 0..9 ) = [i^m | i <- 0..9]
if n == sum( powers(int(d)) | d <- num )
n # narc( n + 1 )
else
narc( n + 1 )
narc( start )
println( narcissistic(0).take(25) )</lang>
{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315]
</pre>
--------------------
Rot-13
--------------------
=={{header|FunL}}==
<lang funl>import io.{lines, stdin}
def rot13( s ) =
buf = StringBuilder()
for c <- s
if isalpha( c )
n = ((ord(c) and 0x1F) - 1 + 13)%26 + 1
buf.append( chr(n or (if isupper(c) then 64 else 96)) )
else
buf.append( c )
buf.toString()
def rot13lines( ls ) =
for l <- ls
println( rot13(l) )
if _name_ == '-main-'
if args.isEmpty()
rot13lines( stdin() )
else
for f <- args
rot13lines( lines(f) )</lang>
--------------------
Set
--------------------
=={{header|FunL}}==
<lang funl>A = {1, 2, 3}
B = {3, 4, 5}
C = {1, 2, 3, 4, 5}
D = {2, 1, 3}
println( '2 is in A: ' + (2 in A) )
println( '4 is in A: ' + (4 in A) )
println( 'A union B: ' + A.union(B) )
println( 'A intersect B: ' + A.intersect(B) )
println( 'A difference B: ' + A.diff(B) )
println( 'A subset of B: ' + A.subsetOf(B) )
println( 'A subset of B: ' + A.subsetOf(C) )
println( 'A equal B: ' + (A == B) )
println( 'A equal D: ' + (A == D) )
S = set( A )
println( 'S (mutable version of A): ' + S )
S.add( 4 )
println( 'S with 4 added: ' + S )
println( 'S subset of C: ' + S.subsetOf(C) )
S.remove( 1 )
println( 'S after 1 removed: ' + S )</lang>
{{out}}
<pre>
2 is in A: true
4 is in A: false
A union B: {4, 5, 1, 2, 3}
A intersect B: {3}
A difference B: {1, 2}
A subset of B: false
A subset of B: true
A equal B: false
A equal D: true
S (mutable version of A): {1, 2, 3}
S with 4 added: {1, 2, 3, 4}
S subset of C: true
S after 1 removed: {2, 3, 4}
</pre>