From 49960d8950d001a372a0abde670a66ee60026957 Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Tue, 5 Mar 2024 18:19:27 +0900 Subject: v0.12.0 --- sample/pseudo_higher_order/map.in | 49 +++++++++++++++++++++++++++++++ sample/pseudo_higher_order/reduce.in | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 sample/pseudo_higher_order/map.in create mode 100644 sample/pseudo_higher_order/reduce.in (limited to 'sample/pseudo_higher_order') diff --git a/sample/pseudo_higher_order/map.in b/sample/pseudo_higher_order/map.in new file mode 100644 index 0000000..c96daae --- /dev/null +++ b/sample/pseudo_higher_order/map.in @@ -0,0 +1,49 @@ +// Map operation + +// target function +inc(r)><(int i) => r~(i+1); +subPair(r)><(int m, int n) => r~(m-n); + + +// ----------- main (with the built-in Map and `%' notation) ----------- +Map(r, %inc) ~ [1,2,3]; +r; // r ->[2,3,4] + +Map(rr, %subPair) ~ [(10,2), (30,13)]; +rr; // r4 ->[8,17] + + + + +// ----------- main (without the built-in Map) ----------- +// map +map(result, f) >< [] => result~[], Eraser~f; +map(result, f) >< x:xs => Dup(f1,f2)~f, + result~w:ws, + f1 ~ (w, x), map(ws, f2)~xs; + + +// --- map application without the `%' notation --- +map(r1, (a, inc(a))) ~ [10,11,12]; +r1; // r1 ->[11,12,13] + +map(r2, (a, subPair(a))) ~ [(10,2), (30,13)]; +r2; // r2 ->[8,17] + + +// --- map application with the `%' notation --- +map(r3, %inc) ~ [1,2,3]; +r3; // r3 ->[2,3,4] + +map(r4, %subPair) ~ [(10,2), (30,13)]; +r4; // r4 ->[8,17] + + +// --- map application with `<<' abbriviation also +// --- It seems the simplest to read. +r5 << map(%inc, [100,200,300]); +r5; // r5 ->[101,201,301] + +r6 << map(%subPair, [(10,2), (30,13)]); +r6; // r6 ->[8,17] + diff --git a/sample/pseudo_higher_order/reduce.in b/sample/pseudo_higher_order/reduce.in new file mode 100644 index 0000000..90ba145 --- /dev/null +++ b/sample/pseudo_higher_order/reduce.in @@ -0,0 +1,57 @@ +// foldr, foldl functions + + +// target functions +// Here, we use built-in agents Add and Sub + + +// -------------------------------------------------------------------- +// foldr f v [x0, x1, ..., xn] = f(x0, f(x1, ... f(xn-1, f(xn, v))...)) +// -------------------------------------------------------------------- +foldr(r, f, v) >< x:xs => foldr_Cons(r,f,v,x)~xs; +foldr_Cons(r,f,v,x) >< [] => f~((r,x),v); +foldr_Cons(r,f,v,x) >< y:ys => + Dup(f1,f2)~f, + foldr(w, f2, v)~y:ys, + f1~((r,x),w); + +// -------------------------------------------------------------------- +// foldl f v [x0, x1, ..., xn] = f( ... f(f(v,x0),x1) ..., xn) +// -------------------------------------------------------------------- +foldl(r, f, v) >< x:xs => foldl_Cons(r,f,v,x)~xs; +foldl_Cons(r,f,v,x) >< [] => f~((r,v),x); +foldl_Cons(r,f,v,x) >< y:ys => + Dup(f1,f2)~f, + f1~((w,v),x), + foldl(r, f2, w)~y:ys; + + + + +// ---------- main ---------- + +// foldr f v [x0, x1, ..., xn] = f(x0, f(x1, ... f(xn-1, f(xn, v))...)) +// foldr plus 10 [1,2,3] = plus(1, plus(2, plus(3, 10))) = 16 + +foldr(r, %Add, 10) ~ [1,2,3]; +r; free r; // r ->16 + + +// foldr subt 1 [30,20,10] = subt(30, subt(20, subt(10, 1))) +// = 30-(20-(10-1)) = 30-(20-9) = 30-11 = 19 + +foldr(r, %Sub, 1) ~ [30,20,10]; +r; free r; // r ->19 + + + +// foldl f v [x0, x1, ..., xn] = f( ... f(f(v,x0),x1) ..., xn) +// foldl plus 10 [1,2,3] = plus(3, plus(2, plus(1, 10))) = 16 + +foldl(r, %Add, 10) ~ [1,2,3]; +r; free r; // r ->16 + + +// foldl subt 10 [1,2,3] = subt(subt(subt(10,1),2),3) = ((10-1)-2)-3 = 4 +foldl(r, %Sub, 10) ~ [1,2,3]; +r; free r; // r ->4 -- cgit v1.2.3