Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
7
Task/Catamorphism/00DESCRIPTION
Normal file
7
Task/Catamorphism/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
''Reduce'' is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
|
||||
|
||||
Show how ''reduce'' (or ''foldl'' or ''foldr'' etc), work (or would be implemented) in your language.
|
||||
|
||||
;Cf.
|
||||
* [[wp:Fold (higher-order function)|Fold]]
|
||||
* [[wp:Catamorphism|Catamorphism]]
|
||||
29
Task/Catamorphism/Ada/catamorphism.ada
Normal file
29
Task/Catamorphism/Ada/catamorphism.ada
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Catamorphism is
|
||||
|
||||
type Fun is access function (Left, Right: Natural) return Natural;
|
||||
type Arr is array(Natural range <>) of Natural;
|
||||
|
||||
function Fold_Left (F: Fun; A: Arr) return Natural is
|
||||
Result: Natural := A(A'First);
|
||||
begin
|
||||
for I in A'First+1 .. A'Last loop
|
||||
Result := F(Result, A(I));
|
||||
end loop;
|
||||
return Result;
|
||||
end Fold_Left;
|
||||
|
||||
function Max (L, R: Natural) return Natural is (if L > R then L else R);
|
||||
function Min (L, R: Natural) return Natural is (if L < R then L else R);
|
||||
function Add (Left, Right: Natural) return Natural is (Left + Right);
|
||||
function Mul (Left, Right: Natural) return Natural is (Left * Right);
|
||||
|
||||
package NIO is new Ada.Text_IO.Integer_IO(Natural);
|
||||
|
||||
begin
|
||||
NIO.Put(Fold_Left(Min'Access, (1,2,3,4)), Width => 3);
|
||||
NIO.Put(Fold_Left(Max'Access, (1,2,3,4)), Width => 3);
|
||||
NIO.Put(Fold_Left(Add'Access, (1,2,3,4)), Width => 3);
|
||||
NIO.Put(Fold_Left(Mul'Access, (1,2,3,4)), Width => 3);
|
||||
end Catamorphism;
|
||||
18
Task/Catamorphism/Bracmat/catamorphism.bracmat
Normal file
18
Task/Catamorphism/Bracmat/catamorphism.bracmat
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
( ( fold
|
||||
= f xs init first rest
|
||||
. !arg:(?f.?xs.?init)
|
||||
& ( !xs:&!init
|
||||
| !xs:%?first ?rest
|
||||
& !f$(!first.fold$(!f.!rest.!init))
|
||||
)
|
||||
)
|
||||
& out
|
||||
$ ( fold
|
||||
$ ( (=a b.!arg:(?a.?b)&!a+!b)
|
||||
. 1 2 3 4 5
|
||||
. 0
|
||||
)
|
||||
)
|
||||
& (product=a b.!arg:(?a.?b)&!a*!b)
|
||||
& out$(fold$(product.1 2 3 4 5.1))
|
||||
);
|
||||
14
Task/Catamorphism/C++/catamorphism.cpp
Normal file
14
Task/Catamorphism/C++/catamorphism.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::vector<int> nums = { 1, 2, 3, 4, 5 };
|
||||
auto nums_added = std::accumulate(std::begin(nums), std::end(nums), 0, std::plus<int>());
|
||||
auto nums_other = std::accumulate(std::begin(nums), std::end(nums), 0, [](const int& a, const int& b) {
|
||||
return a + 2 * b;
|
||||
});
|
||||
std::cout << "nums_added: " << nums_added << std::endl;
|
||||
std::cout << "nums_other: " << nums_other << std::endl;
|
||||
}
|
||||
9
Task/Catamorphism/C-sharp/catamorphism.cs
Normal file
9
Task/Catamorphism/C-sharp/catamorphism.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
var nums = Enumerable.Range(1, 10);
|
||||
|
||||
int summation = nums.Aggregate((a, b) => a + b);
|
||||
|
||||
int product = nums.Aggregate((a, b) => a * b);
|
||||
|
||||
string concatenation = nums.Aggregate(String.Empty, (a, b) => a.ToString() + b.ToString());
|
||||
|
||||
Console.WriteLine("{0} {1} {2}", summation, product, concatenation);
|
||||
24
Task/Catamorphism/C/catamorphism.c
Normal file
24
Task/Catamorphism/C/catamorphism.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
|
||||
typedef int (*intFn)(int, int);
|
||||
|
||||
int reduce(intFn fn, int size, int *elms)
|
||||
{
|
||||
int i, val = *elms;
|
||||
for (i = 1; i < size; ++i)
|
||||
val = fn(val, elms[i]);
|
||||
return val;
|
||||
}
|
||||
|
||||
int add(int a, int b) { return a + b; }
|
||||
int sub(int a, int b) { return a - b; }
|
||||
int mul(int a, int b) { return a * b; }
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int nums[] = {1, 2, 3, 4, 5};
|
||||
printf("%d\n", reduce(add, 5, nums));
|
||||
printf("%d\n", reduce(sub, 5, nums));
|
||||
printf("%d\n", reduce(mul, 5, nums));
|
||||
return 0;
|
||||
}
|
||||
6
Task/Catamorphism/Clojure/catamorphism.clj
Normal file
6
Task/Catamorphism/Clojure/catamorphism.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
; Basic usage
|
||||
> (reduce * '(1 2 3 4 5))
|
||||
120
|
||||
; Using an initial value
|
||||
> (reduce + 100 '(1 2 3 4 5))
|
||||
115
|
||||
18
Task/Catamorphism/Common-Lisp/catamorphism.lisp
Normal file
18
Task/Catamorphism/Common-Lisp/catamorphism.lisp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
; Basic usage
|
||||
> (reduce #'* '(1 2 3 4 5))
|
||||
120
|
||||
; Using an initial value
|
||||
> (reduce #'+ '(1 2 3 4 5) :initial-value 100)
|
||||
115
|
||||
; Using only a subsequence
|
||||
> (reduce #'+ '(1 2 3 4 5) :start 1 :end 4)
|
||||
9
|
||||
; Apply a function to each element first
|
||||
> (reduce #'+ '((a 1) (b 2) (c 3)) :key #'cadr)
|
||||
6
|
||||
; Right-associative reduction
|
||||
> (reduce #'expt '(2 3 4) :from-end T)
|
||||
2417851639229258349412352
|
||||
; Compare with
|
||||
> (reduce #'expt '(2 3 4))
|
||||
4096
|
||||
13
Task/Catamorphism/D/catamorphism.d
Normal file
13
Task/Catamorphism/D/catamorphism.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
void main() {
|
||||
import std.stdio, std.algorithm, std.range, std.numeric,
|
||||
std.conv, std.typecons, std.typetuple;
|
||||
|
||||
auto list = iota(1, 11);
|
||||
alias ops = TypeTuple!(q{a + b}, q{a * b}, min, max, gcd);
|
||||
|
||||
foreach (op; ops)
|
||||
writeln(op.stringof, ": ", list.reduce!op);
|
||||
|
||||
// std.algorithm.reduce supports multiple functions in parallel:
|
||||
reduce!(ops[0], ops[3], text)(tuple(0, 0.0, ""), list).writeln;
|
||||
}
|
||||
8
Task/Catamorphism/Deja-Vu/catamorphism.djv
Normal file
8
Task/Catamorphism/Deja-Vu/catamorphism.djv
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
reduce f lst init:
|
||||
if lst:
|
||||
f reduce @f lst init pop-from lst
|
||||
else:
|
||||
init
|
||||
|
||||
!. reduce @+ [ 1 10 200 ] 4
|
||||
!. reduce @- [ 1 10 200 ] 4
|
||||
25
Task/Catamorphism/Go/catamorphism.go
Normal file
25
Task/Catamorphism/Go/catamorphism.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
n := []int{1, 2, 3, 4, 5}
|
||||
|
||||
fmt.Println(reduce(add, n))
|
||||
fmt.Println(reduce(sub, n))
|
||||
fmt.Println(reduce(mul, n))
|
||||
}
|
||||
|
||||
func add(a int, b int) int { return a + b }
|
||||
func sub(a int, b int) int { return a - b }
|
||||
func mul(a int, b int) int { return a * b }
|
||||
|
||||
func reduce(rf func(int, int) int, m []int) int {
|
||||
r := m[0]
|
||||
for _, v := range m[1:] {
|
||||
r = rf(r, v)
|
||||
}
|
||||
return r
|
||||
}
|
||||
5
Task/Catamorphism/Haskell/catamorphism.hs
Normal file
5
Task/Catamorphism/Haskell/catamorphism.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
nums = [1..10]
|
||||
|
||||
summation = foldl (+) 0 nums
|
||||
product = foldl (*) 1 nums
|
||||
concatenation = foldr (\num s -> show num ++ s) "" nums
|
||||
9
Task/Catamorphism/Icon/catamorphism.icon
Normal file
9
Task/Catamorphism/Icon/catamorphism.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure main(A)
|
||||
write(A[1],": ",curry(A[1],A[2:0]))
|
||||
end
|
||||
|
||||
procedure curry(f,A)
|
||||
r := A[1]
|
||||
every r := f(r, !A[2:0])
|
||||
return r
|
||||
end
|
||||
1
Task/Catamorphism/J/catamorphism-1.j
Normal file
1
Task/Catamorphism/J/catamorphism-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
/
|
||||
6
Task/Catamorphism/J/catamorphism-2.j
Normal file
6
Task/Catamorphism/J/catamorphism-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
+/ 1 2 3 4 5
|
||||
15
|
||||
*/ 1 2 3 4 5
|
||||
120
|
||||
!/ 1 2 3 4 5 NB. "n ! k" is "n choose k"
|
||||
45
|
||||
9
Task/Catamorphism/Java/catamorphism.java
Normal file
9
Task/Catamorphism/Java/catamorphism.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import java.util.stream.Stream;
|
||||
|
||||
public class ReduceTask {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Stream.of(1, 2, 3, 4, 5).mapToInt(i -> i).sum());
|
||||
System.out.println(Stream.of(1, 2, 3, 4, 5).reduce(1, (a, b) -> a * b));
|
||||
}
|
||||
}
|
||||
17
Task/Catamorphism/JavaScript/catamorphism.js
Normal file
17
Task/Catamorphism/JavaScript/catamorphism.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
|
||||
function add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
var summation = nums.reduce(add);
|
||||
|
||||
function mul(a, b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
var product = nums.reduce(mul, 1);
|
||||
|
||||
var concatenation = nums.reduce(add, "");
|
||||
|
||||
console.log(summation, product, concatenation);
|
||||
27
Task/Catamorphism/LOLCODE/catamorphism.lol
Normal file
27
Task/Catamorphism/LOLCODE/catamorphism.lol
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
HAI 1.3
|
||||
|
||||
HOW IZ I reducin YR array AN YR size AN YR fn
|
||||
I HAS A val ITZ array'Z SRS 0
|
||||
IM IN YR loop UPPIN YR i TIL BOTH SAEM i AN DIFF OF size AN 1
|
||||
val R I IZ fn YR val AN YR array'Z SRS SUM OF i AN 1 MKAY
|
||||
IM OUTTA YR loop
|
||||
FOUND YR val
|
||||
IF U SAY SO
|
||||
|
||||
O HAI IM array
|
||||
I HAS A SRS 0 ITZ 1
|
||||
I HAS A SRS 1 ITZ 2
|
||||
I HAS A SRS 2 ITZ 3
|
||||
I HAS A SRS 3 ITZ 4
|
||||
I HAS A SRS 4 ITZ 5
|
||||
KTHX
|
||||
|
||||
HOW IZ I add YR a AN YR b, FOUND YR SUM OF a AN b, IF U SAY SO
|
||||
HOW IZ I sub YR a AN YR b, FOUND YR DIFF OF a AN b, IF U SAY SO
|
||||
HOW IZ I mul YR a AN YR b, FOUND YR PRODUKT OF a AN b, IF U SAY SO
|
||||
|
||||
VISIBLE I IZ reducin YR array AN YR 5 AN YR add MKAY
|
||||
VISIBLE I IZ reducin YR array AN YR 5 AN YR sub MKAY
|
||||
VISIBLE I IZ reducin YR array AN YR 5 AN YR mul MKAY
|
||||
|
||||
KTHXBYE
|
||||
14
Task/Catamorphism/Logtalk/catamorphism.logtalk
Normal file
14
Task/Catamorphism/Logtalk/catamorphism.logtalk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
:- object(folding_examples).
|
||||
|
||||
:- public(show/0).
|
||||
show :-
|
||||
integer::sequence(1, 10, List),
|
||||
write('List: '), write(List), nl,
|
||||
meta::fold_left([Acc,N,Sum0]>>(Sum0 is Acc+N), 0, List, Sum),
|
||||
write('Sum of all elements: '), write(Sum), nl,
|
||||
meta::fold_left([Acc,N,Product0]>>(Product0 is Acc*N), 1, List, Product),
|
||||
write('Product of all elements: '), write(Product), nl,
|
||||
meta::fold_left([Acc,N,Concat0]>>(number_codes(N,NC), atom_codes(NA,NC), atom_concat(Acc,NA,Concat0)), '', List, Concat),
|
||||
write('Concatenation of all elements: '), write(Concat), nl.
|
||||
|
||||
:- end_object.
|
||||
8
Task/Catamorphism/Maple/catamorphism-1.maple
Normal file
8
Task/Catamorphism/Maple/catamorphism-1.maple
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> nums := seq( 1 .. 10 );
|
||||
nums := 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
||||
|
||||
> foldl( `+`, 0, nums ); # compute sum using foldl
|
||||
55
|
||||
|
||||
> foldr( `*`, 1, nums ); # compute product using foldr
|
||||
3628800
|
||||
2
Task/Catamorphism/Maple/catamorphism-2.maple
Normal file
2
Task/Catamorphism/Maple/catamorphism-2.maple
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> foldl( (a,b) ->a*T+b, op(map2(op,1,[op( 72*T^5+37*T^4-23*T^3+87*T^2+44*T+29 )])));
|
||||
((((72 T + 37) T - 23) T + 87) T + 44) T + 29
|
||||
1
Task/Catamorphism/Mathematica/catamorphism.math
Normal file
1
Task/Catamorphism/Mathematica/catamorphism.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Fold[f, x, {a, b, c, d}]
|
||||
2
Task/Catamorphism/Maxima/catamorphism-1.maxima
Normal file
2
Task/Catamorphism/Maxima/catamorphism-1.maxima
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
lreduce(f, [a, b, c, d], x0);
|
||||
/* (%o1) f(f(f(f(x0, a), b), c), d) */
|
||||
2
Task/Catamorphism/Maxima/catamorphism-2.maxima
Normal file
2
Task/Catamorphism/Maxima/catamorphism-2.maxima
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
lreduce("+", [1, 2, 3, 4], 100);
|
||||
/* (%o1) 110 */
|
||||
2
Task/Catamorphism/Nemerle/catamorphism.nemerle
Normal file
2
Task/Catamorphism/Nemerle/catamorphism.nemerle
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def seq = [1, 4, 6, 3, 7];
|
||||
def sum = seq.Fold(0, _ + _); // Fold takes an initial value and a function, here the + operator
|
||||
6
Task/Catamorphism/OCaml/catamorphism.ocaml
Normal file
6
Task/Catamorphism/OCaml/catamorphism.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# let nums = [1;2;3;4;5;6;7;8;9;10];;
|
||||
val nums : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
|
||||
# let sum = List.fold_left (+) 0 nums;;
|
||||
val sum : int = 55
|
||||
# let product = List.fold_left ( * ) 1 nums;;
|
||||
val product : int = 3628800
|
||||
72
Task/Catamorphism/Oberon-2/catamorphism.oberon-2
Normal file
72
Task/Catamorphism/Oberon-2/catamorphism.oberon-2
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
MODULE Catamorphism;
|
||||
IMPORT
|
||||
Object,
|
||||
NPCT:Tools,
|
||||
NPCT:Args,
|
||||
IntStr,
|
||||
Out;
|
||||
|
||||
TYPE
|
||||
BinaryFunc= PROCEDURE (x,y: LONGINT): LONGINT;
|
||||
|
||||
VAR
|
||||
data: POINTER TO ARRAY OF LONGINT;
|
||||
i: LONGINT;
|
||||
|
||||
PROCEDURE Sum(x,y: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
RETURN x + y
|
||||
END Sum;
|
||||
|
||||
PROCEDURE Sub(x,y: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
RETURN x - y;
|
||||
END Sub;
|
||||
|
||||
PROCEDURE Mul(x,y: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
RETURN x * y;
|
||||
END Mul;
|
||||
|
||||
PROCEDURE Reduce(x: ARRAY OF LONGINT; f: BinaryFunc): LONGINT;
|
||||
VAR
|
||||
i,res: LONGINT;
|
||||
BEGIN
|
||||
res := x[0];i := 1;
|
||||
WHILE (i < LEN(x)) DO;
|
||||
res := f(res,x[i]);
|
||||
INC(i)
|
||||
END;
|
||||
RETURN res
|
||||
END Reduce;
|
||||
|
||||
PROCEDURE InitData(VAR x: ARRAY OF LONGINT);
|
||||
VAR
|
||||
i, j: LONGINT;
|
||||
res: IntStr.ConvResults;
|
||||
aux: Object.CharsLatin1;
|
||||
BEGIN
|
||||
i := 0;j := 1;
|
||||
WHILE (j <= LEN(x)) DO
|
||||
aux := Tools.AsString(Args.Get(j));
|
||||
IntStr.StrToInt(aux^,x[i],res);
|
||||
IF res # IntStr.strAllRight THEN
|
||||
Out.String("Incorrect format for data at index ");Out.LongInt(j,0);Out.Ln;
|
||||
HALT(1);
|
||||
END;
|
||||
INC(j);INC(i)
|
||||
END
|
||||
END InitData;
|
||||
|
||||
BEGIN
|
||||
IF Args.Number() = 1 THEN
|
||||
Out.String("Invalid number of arguments. ");Out.Ln;
|
||||
HALT(0)
|
||||
ELSE
|
||||
NEW(data,Args.Number() - 1);
|
||||
InitData(data^);
|
||||
Out.LongInt(Reduce(data^,Sum),0);Out.Ln;
|
||||
Out.LongInt(Reduce(data^,Sub),0);Out.Ln;
|
||||
Out.LongInt(Reduce(data^,Mul),0);Out.Ln
|
||||
END
|
||||
END Catamorphism.
|
||||
6
Task/Catamorphism/PARI-GP/catamorphism-1.pari
Normal file
6
Task/Catamorphism/PARI-GP/catamorphism-1.pari
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
reduce(f, v)={
|
||||
my(t=v[1]);
|
||||
for(i=2,#v,t=f(t,v[i]));
|
||||
t
|
||||
};
|
||||
reduce((a,b)->a+b, [1,2,3,4,5,6,7,8,9,10])
|
||||
1
Task/Catamorphism/PARI-GP/catamorphism-2.pari
Normal file
1
Task/Catamorphism/PARI-GP/catamorphism-2.pari
Normal file
|
|
@ -0,0 +1 @@
|
|||
fold((a,b)->a+b, [1..10])
|
||||
53
Task/Catamorphism/Pascal/catamorphism.pascal
Normal file
53
Task/Catamorphism/Pascal/catamorphism.pascal
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
program reduce;
|
||||
|
||||
type
|
||||
// tmyArray = array of LongInt;
|
||||
tmyArray = array[-5..5] of LongInt;
|
||||
tmyFunc = function (a,b:LongInt):LongInt;
|
||||
|
||||
function add(x,y:LongInt):LongInt;
|
||||
begin
|
||||
add := x+y;
|
||||
end;
|
||||
|
||||
function sub(k,l:LongInt):LongInt;
|
||||
begin
|
||||
sub := k-l;
|
||||
end;
|
||||
|
||||
function mul(r,t:LongInt):LongInt;
|
||||
begin
|
||||
mul := r*t;
|
||||
end;
|
||||
|
||||
function reduce(myFunc:tmyFunc;a:tmyArray):LongInt;
|
||||
var
|
||||
i,res : LongInt;
|
||||
begin
|
||||
res := a[low(a)];
|
||||
For i := low(a)+1 to high(a) do
|
||||
res := myFunc(res,a[i]);
|
||||
reduce := res;
|
||||
end;
|
||||
|
||||
procedure InitMyArray(var a:tmyArray);
|
||||
var
|
||||
i: LongInt;
|
||||
begin
|
||||
For i := low(a) to high(a) do
|
||||
begin
|
||||
//no a[i] = 0
|
||||
a[i] := i + ord(i=0);
|
||||
write(a[i],',');
|
||||
end;
|
||||
writeln(#8#32);
|
||||
end;
|
||||
|
||||
var
|
||||
ma : tmyArray;
|
||||
BEGIN
|
||||
InitMyArray(ma);
|
||||
writeln(reduce(@add,ma));
|
||||
writeln(reduce(@sub,ma));
|
||||
writeln(reduce(@mul,ma));
|
||||
END.
|
||||
7
Task/Catamorphism/Perl-6/catamorphism-1.pl6
Normal file
7
Task/Catamorphism/Perl-6/catamorphism-1.pl6
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
my @list = 1..10;
|
||||
say [+] @list;
|
||||
say [*] @list;
|
||||
say [~] @list;
|
||||
say [min] @list;
|
||||
say [max] @list;
|
||||
say [lcm] @list;
|
||||
6
Task/Catamorphism/Perl-6/catamorphism-2.pl6
Normal file
6
Task/Catamorphism/Perl-6/catamorphism-2.pl6
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
say reduce &infix:<+>, @list;
|
||||
say reduce &infix:<*>, @list;
|
||||
say reduce &infix:<~>, @list;
|
||||
say reduce &infix:<min>, @list;
|
||||
say reduce &infix:<max>, @list;
|
||||
say reduce &infix:<lcm>, @list;
|
||||
8
Task/Catamorphism/Perl/catamorphism.pl
Normal file
8
Task/Catamorphism/Perl/catamorphism.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use List::Util 'reduce';
|
||||
|
||||
# note the use of the odd $a and $b globals
|
||||
print +(reduce {$a + $b} 1 .. 10), "\n";
|
||||
|
||||
# first argument is really an anon function; you could also do this:
|
||||
sub func { $b & 1 ? "$a $b" : "$b $a" }
|
||||
print +(reduce \&func, 1 .. 10), "\n"
|
||||
11
Task/Catamorphism/PicoLisp/catamorphism.l
Normal file
11
Task/Catamorphism/PicoLisp/catamorphism.l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(de reduce ("Fun" "Lst")
|
||||
(let "A" (car "Lst")
|
||||
(for "N" (cdr "Lst")
|
||||
(setq "A" ("Fun" "A" "N")) )
|
||||
"A" ) )
|
||||
|
||||
(println
|
||||
(reduce + (1 2 3 4 5))
|
||||
(reduce * (1 2 3 4 5)) )
|
||||
|
||||
(bye)
|
||||
14
Task/Catamorphism/Prolog/catamorphism.pro
Normal file
14
Task/Catamorphism/Prolog/catamorphism.pro
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
:- use_module(library(lambda)).
|
||||
|
||||
% foldl is now a predicate of SWI-Prolog 6.3.1
|
||||
%
|
||||
catamorphism :-
|
||||
numlist(1,10,L),
|
||||
foldl(\XS^YS^ZS^(ZS is XS+YS), L, 0, Sum),
|
||||
format('Sum of ~w is ~w~n', [L, Sum]),
|
||||
foldl(\XP^YP^ZP^(ZP is XP*YP), L, 1, Prod),
|
||||
format('Prod of ~w is ~w~n', [L, Prod]),
|
||||
string_to_list(LV, ""),
|
||||
foldl(\XC^YC^ZC^(string_to_atom(XS, XC),string_concat(YC,XS,ZC)),
|
||||
L, LV, Concat),
|
||||
format('Concat of ~w is ~w~n', [L, Concat]).
|
||||
18
Task/Catamorphism/Python/catamorphism-1.py
Normal file
18
Task/Catamorphism/Python/catamorphism-1.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
>>> from operator import add
|
||||
>>> listoflists = [['the', 'cat'], ['sat', 'on'], ['the', 'mat']]
|
||||
>>> help(reduce)
|
||||
Help on built-in function reduce in module __builtin__:
|
||||
|
||||
reduce(...)
|
||||
reduce(function, sequence[, initial]) -> value
|
||||
|
||||
Apply a function of two arguments cumulatively to the items of a sequence,
|
||||
from left to right, so as to reduce the sequence to a single value.
|
||||
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
|
||||
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
|
||||
of the sequence in the calculation, and serves as a default when the
|
||||
sequence is empty.
|
||||
|
||||
>>> reduce(add, listoflists, [])
|
||||
['the', 'cat', 'sat', 'on', 'the', 'mat']
|
||||
>>>
|
||||
12
Task/Catamorphism/Python/catamorphism-2.py
Normal file
12
Task/Catamorphism/Python/catamorphism-2.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from functools import reduce
|
||||
from operator import add, mul
|
||||
|
||||
nums = range(1,11)
|
||||
|
||||
summation = reduce(add, nums)
|
||||
|
||||
product = reduce(mul, nums)
|
||||
|
||||
concatenation = reduce(lambda a, b: str(a) + str(b), nums)
|
||||
|
||||
print(summation, product, concatenation)
|
||||
1
Task/Catamorphism/README
Normal file
1
Task/Catamorphism/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Catamorphism
|
||||
36
Task/Catamorphism/REXX/catamorphism.rexx
Normal file
36
Task/Catamorphism/REXX/catamorphism.rexx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*REXX pgm shows a method for catamorphism for some simple functions. */
|
||||
@list = 1 2 3 4 5 6 7 8 9 10
|
||||
say 'show:' fold(@list, 'show')
|
||||
say ' sum:' fold(@list, '+')
|
||||
say 'prod:' fold(@list, '*')
|
||||
say ' cat:' fold(@list, '||')
|
||||
say ' min:' fold(@list, 'min')
|
||||
say ' max:' fold(@list, 'max')
|
||||
say ' avg:' fold(@list, 'avg')
|
||||
say ' GCD:' fold(@list, 'GCD')
|
||||
say ' LCM:' fold(@list, 'LCM')
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────FOLD subroutine─────────────────────*/
|
||||
fold: procedure; parse arg z; arg ,f; z=space(z) /*F is uppercased.*/
|
||||
za=translate(z, f, " "); zf=f'('translate(z,',' ," ")')'
|
||||
if f=='+' | f=='*' then interpret 'return' za
|
||||
if f=='||' then return space(z,0)
|
||||
if f=='AVG' then interpret 'return' fold(z,'+') "/" words(z)
|
||||
if wordpos(f,'MIN MAX LCM GCD')\==0 then interpret 'return' zf
|
||||
if f=='SHOW' then return z
|
||||
return 'illegal function:' arg(2)
|
||||
/*──────────────────────────────────LCM subroutine──────────────────────*/
|
||||
lcm: procedure; $=; do j=1 for arg(); $=$ arg(j); end
|
||||
x=abs(word($,1)) /* [↑] build a list of arguments.*/
|
||||
do k=2 to words($); !=abs(word($,k)); if !=0 then return 0
|
||||
x=x*! / gcd(x,!) /*have GCD do the heavy lifting.*/
|
||||
end /*k*/
|
||||
return x /*return with the money. */
|
||||
/*──────────────────────────────────GCD subroutine──────────────────────*/
|
||||
gcd: procedure; $=; do j=1 for arg(); $=$ arg(j); end
|
||||
parse var $ x z .; if x=0 then x=z /* [↑] build a list of arguments.*/
|
||||
x=abs(x)
|
||||
do k=2 to words($); y=abs(word($,k)); if y=0 then iterate
|
||||
do until _==0; _=x//y; x=y; y=_; end /*until*/
|
||||
end /*k*/
|
||||
return x /*return with the money. */
|
||||
8
Task/Catamorphism/Racket/catamorphism.rkt
Normal file
8
Task/Catamorphism/Racket/catamorphism.rkt
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#lang racket
|
||||
(define (fold f xs init)
|
||||
(if (empty? xs)
|
||||
init
|
||||
(f (first xs)
|
||||
(fold f (rest xs) init))))
|
||||
|
||||
(fold + '(1 2 3) 0) ; the result is 6
|
||||
4
Task/Catamorphism/Ruby/catamorphism-1.rb
Normal file
4
Task/Catamorphism/Ruby/catamorphism-1.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# sum:
|
||||
p (1..10).inject(:+)
|
||||
# smallest number divisible by all numbers from 1 to 20:
|
||||
p (1..20).inject(:lcm) #lcm: lowest common multiple
|
||||
11
Task/Catamorphism/Ruby/catamorphism-2.rb
Normal file
11
Task/Catamorphism/Ruby/catamorphism-2.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
p row = [1]
|
||||
10.times{p row = row.each_cons(2).inject([1,1]){|ar,(a,b)| ar.insert(-2, a+b)} }
|
||||
|
||||
# [1]
|
||||
# [1, 1]
|
||||
# [1, 2, 1]
|
||||
# [1, 3, 3, 1]
|
||||
# [1, 4, 6, 4, 1]
|
||||
# [1, 5, 10, 10, 5, 1]
|
||||
# [1, 6, 15, 20, 15, 6, 1]
|
||||
# etc
|
||||
28
Task/Catamorphism/Run-BASIC/catamorphism.run
Normal file
28
Task/Catamorphism/Run-BASIC/catamorphism.run
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
for i = 1 to 10 :n(i) = i:next i
|
||||
|
||||
print " +: ";" ";cat(10,"+")
|
||||
print " -: ";" ";cat(10,"-")
|
||||
print " *: ";" ";cat(10,"*")
|
||||
print " /: ";" ";cat(10,"/")
|
||||
print " ^: ";" ";cat(10,"^")
|
||||
print "min: ";" ";cat(10,"min")
|
||||
print "max: ";" ";cat(10,"max")
|
||||
print "avg: ";" ";cat(10,"avg")
|
||||
print "cat: ";" ";cat(10,"cat")
|
||||
|
||||
function cat(count,op$)
|
||||
cat = n(1)
|
||||
for i = 2 to count
|
||||
if op$ = "+" then cat = cat + n(i)
|
||||
if op$ = "-" then cat = cat - n(i)
|
||||
if op$ = "*" then cat = cat * n(i)
|
||||
if op$ = "/" then cat = cat / n(i)
|
||||
if op$ = "^" then cat = cat ^ n(i)
|
||||
if op$ = "max" then cat = max(cat,n(i))
|
||||
if op$ = "min" then cat = min(cat,n(i))
|
||||
if op$ = "avg" then cat = cat + n(i)
|
||||
if op$ = "cat" then cat$ = cat$ + str$(n(i))
|
||||
next i
|
||||
if op$ = "avg" then cat = cat / count
|
||||
if op$ = "cat" then cat = val(str$(n(1))+cat$)
|
||||
end function
|
||||
6
Task/Catamorphism/Standard-ML/catamorphism.ml
Normal file
6
Task/Catamorphism/Standard-ML/catamorphism.ml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
- val nums = [1,2,3,4,5,6,7,8,9,10];
|
||||
val nums = [1,2,3,4,5,6,7,8,9,10] : int list
|
||||
- val sum = foldl op+ 0 nums;
|
||||
val sum = 55 : int
|
||||
- val product = foldl op* 1 nums;
|
||||
val product = 3628800 : int
|
||||
7
Task/Catamorphism/Tcl/catamorphism-1.tcl
Normal file
7
Task/Catamorphism/Tcl/catamorphism-1.tcl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
proc fold {lambda zero list} {
|
||||
set accumulator $zero
|
||||
foreach item $list {
|
||||
set accumulator [apply $lambda $accumulator $item]
|
||||
}
|
||||
return $accumulator
|
||||
}
|
||||
5
Task/Catamorphism/Tcl/catamorphism-2.tcl
Normal file
5
Task/Catamorphism/Tcl/catamorphism-2.tcl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
set 1to5 {1 2 3 4 5}
|
||||
|
||||
puts [fold {{a b} {expr {$a+$b}}} 0 $1to5]
|
||||
puts [fold {{a b} {expr {$a*$b}}} 1 $1to5]
|
||||
puts [fold {{a b} {return $a,$b}} x $1to5]
|
||||
3
Task/Catamorphism/Tcl/catamorphism-3.tcl
Normal file
3
Task/Catamorphism/Tcl/catamorphism-3.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
puts [::tcl::mathop::+ {*}$1to5]
|
||||
puts [::tcl::mathop::* {*}$1to5]
|
||||
puts x,[join $1to5 ,]
|
||||
Loading…
Add table
Add a link
Reference in a new issue