aboutsummaryrefslogtreecommitdiff
path: root/sample/msort.in
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2021-09-24 16:42:54 +0900
committerShinya Sato <shinnya.sato@gmail.com>2021-09-24 16:42:54 +0900
commit6b3075d90d945d5e7e8fd4d28284ffd99dbeb82e (patch)
tree4d9e58166e36452bc3a19172d9cfb7a245f996b0 /sample/msort.in
parentdd62122ee43d2c388fb96518eaa527976fddfc38 (diff)
downloadinpla-6b3075d90d945d5e7e8fd4d28284ffd99dbeb82e.tar.gz
inpla-6b3075d90d945d5e7e8fd4d28284ffd99dbeb82e.zip
v0.4.1
Diffstat (limited to '')
-rw-r--r--sample/msort.in28
1 files changed, 28 insertions, 0 deletions
diff --git a/sample/msort.in b/sample/msort.in
new file mode 100644
index 0000000..599990b
--- /dev/null
+++ b/sample/msort.in
@@ -0,0 +1,28 @@
+// Merge sort
+
+// Rules
+msort(ret) >< [] => ret~[];
+msort(ret) >< x:xs => ms_tail(ret, x)~xs;
+
+ms_tail(ret, n) >< [] => ret~[n];
+ms_tail(ret, n) >< x:xs =>
+ split(left,right) ~ (n:x:xs),
+ msort(a)~left, msort(b)~right, merge(ret,b)~a;
+
+merge(ret, snd) >< [] => ret~snd;
+merge(ret, snd) >< x:xs => mergeCC(ret, x, xs)~snd;
+
+mergeCC(ret, int y, ys) >< [] => ret~(y:ys);
+mergeCC(ret, int y, ys) >< (int x):xs
+| x <= y => ret~(x:cnt), mergeCC(cnt, y, ys) ~ xs
+| _ => ret~(y:cnt), mergeCC(cnt, x, xs) ~ ys;
+
+
+split(right,left) >< [] => right~[], left~[];
+split(right,left) >< x:xs =>
+ right~(x:cntl), left~cntr, split(cntr,cntl)~xs;
+
+
+// Nets
+msort(ret)~[3,6,1,9,2];
+ret; \ No newline at end of file