From 1d9b1a6ccb7dfc452cf7da431bc30866b27c1ad5 Mon Sep 17 00:00:00 2001 From: - <@> Date: Tue, 22 Aug 2017 23:05:59 +0300 Subject: [PATCH] =?UTF-8?q?swapped=20monadic=20=E2=86=91=20and=20=E2=8A=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apl.js | 27 ++++++++++++++++----------- examples/5-rule30.apl | 2 +- examples/6-queens.apl | 14 +++++++------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/apl.js b/apl.js index ca1ae3a..578336c 100755 --- a/apl.js +++ b/apl.js @@ -8,17 +8,21 @@ const prelude=[ // (2 2⍴⍳4)~2 !!! RANK ERROR "~←~⍠{(~⍺∊⍵)/⍺}", "_atop←{⍶⍹⍵;⍶⍺⍹⍵}", // (-⍟)2 3←→-⍟2 3 ⍙ 2(-*)3←→-2*3 -// ⊃3←→3 ⍙ ⊃(1 2)(3 4)←→2 2⍴1 2 3 4 ⍙ ⊃(1 2)(3 4 5)←→2 3⍴1 2 0 3 4 5 ⍙ ⊃1 2←→1 2 ⍙ ⊃(1 2)3←→2 2⍴1 2 3 0 -// ⊃1(2 3)←→2 2⍴1 0 2 3 ⍙ ⊃2 2⍴1(1 1 2⍴3 4)(5 6)(2 0⍴0)←→2 2 1 2 2⍴1 0 0 0 3 4 0 0 5 6 0 0 0 0 0 0 ⍙ ⊃⍬←→⍬ -// ⊃2 3 0⍴0←→2 3 0⍴0 ⍙ ⍬⊃3←→3 ⍙ 2⊃'pick'←→'c' ⍙ (⊂1 0)⊃2 2⍴'abcd'←→'c' ⍙ 1⊃'foo' 'bar'←→'bar' ⍙ 1 2⊃'foo' 'bar'←→'r' +// ↑3←→3 ⍙ ↑(1 2)(3 4)←→2 2⍴1 2 3 4 ⍙ ↑(1 2)(3 4 5)←→2 3⍴1 2 0 3 4 5 ⍙ ↑1 2←→1 2 ⍙ ↑(1 2)3←→2 2⍴1 2 3 0 +// ↑1(2 3)←→2 2⍴1 0 2 3 ⍙ ↑2 2⍴1(1 1 2⍴3 4)(5 6)(2 0⍴0)←→2 2 1 2 2⍴1 0 0 0 3 4 0 0 5 6 0 0 0 0 0 0 ⍙ ↑⍬←→⍬ +// ↑2 3 0⍴0←→2 3 0⍴0 +// ⍬⊃3←→3 ⍙ 2⊃'pick'←→'c' ⍙ (⊂1 0)⊃2 2⍴'abcd'←→'c' ⍙ 1⊃'foo' 'bar'←→'bar' ⍙ 1 2⊃'foo' 'bar'←→'r' // (2 2⍴0)⊃1 2 !!! RANK ERROR ⍙ (⊂2 1⍴0)⊃2 2⍴0 !!! RANK ERROR ⍙ (⊂2 2⍴0)⊃1 2 !!! RANK ERROR // (⊂2 2)⊃1 2 !!! RANK ERROR ⍙ (⊂0 2)⊃2 2⍴'ABCD' !!! INDEX ERROR -"⊃←{0=⍴⍴⍵:↑⍵ ⋄ 0=×/⍴⍵:⍵ ⋄ shape←⍴⍵ ⋄ ⍵←,⍵ ⋄ r←⌈/≢¨shapes←⍴¨⍵ ⋄ max←↑⌈/shapes←(⍴↓(r⍴1)∘,)¨shapes", -" (shape,max)⍴↑⍪/shapes{max↑⍺⍴⍵}¨⍵", -" ;", -" 1<⍴⍴⍺:↗'RANK ERROR' ⋄ x←⍵", -" {1<⍴⍴⍵:↗'RANK ERROR' ⋄ ⍵←,⍵ ⋄ (⍴⍵)≠⍴⍴x:↗'RANK ERROR' ⋄ ∨/⍵≥⍴x:↗'INDEX ERROR' ⋄ x←⊃⍵⌷x}¨⍺", -" x}", +"↑←{", +" 0=⍴⍴⍵:⊃⍵ ⋄ 0=×/⍴⍵:⍵ ⋄ shape←⍴⍵ ⋄ ⍵←,⍵ ⋄ r←⌈/≢¨shapes←⍴¨⍵ ⋄ max←⊃⌈/shapes←(⍴↓(r⍴1)∘,)¨shapes", +" (shape,max)⍴⊃⍪/shapes{max↑⍺⍴⍵}¨⍵", +"}⍠↑", +"⊃←⊃⍠{;", +" 1<⍴⍴⍺:↗'RANK ERROR' ⋄ x←⍵", +" {1<⍴⍴⍵:↗'RANK ERROR' ⋄ ⍵←,⍵ ⋄ (⍴⍵)≠⍴⍴x:↗'RANK ERROR' ⋄ ∨/⍵≥⍴x:↗'INDEX ERROR' ⋄ x←⊃⍵⌷x}¨⍺", +" x", +"}", // a←' this is a test '⋄(a≠' ')⊂a←→'this' 'is' (,'a') 'test' "⊂←⊂⍠{1<⍴⍴⍺:↗'RANK ERROR' ⋄ 1≠⍴⍴⍵:↗'NONCE ERROR' ⋄ ⍺←,⍺=0", " keep←~1 1⍷⍺ ⋄ sel←keep/⍺ ⋄ dat←keep/⍵", @@ -1358,7 +1362,8 @@ voc._substitute=args=>{ return A(r,y.s) } -voc['↑']=(y,x)=>x?take(y,x):first(y) +voc['↑']=(y,x)=>x?take(y,x):nyiErr() +voc['⊃']=(y,x)=>x?nyiErr:first(y) // 2↑⎕a←→'AB' ⍙ ¯3↑⎕a←→'XYZ' ⍙ 5↑'abc'←→'abc ' ⍙ ¯5↑'abc'←→' abc' ⍙ 3↑⍳2←→0 1 0 ⍙ ¯1↑⍳4←→,3 // ⍴1↑(2 2⍴⍳4)(⍳10)←→,1 ⍙ 2↑1←→1 0 ⍙ 2 ¯2↑1 1⍴1←→2 2⍴0 1 0 0 ⍙ 3 3↑1 1⍴'a'←→3 3⍴'a ' @@ -1391,7 +1396,7 @@ const take=(x,y)=>{ return A(r,s) } -// ↑(1 2 3)(4 5 6)←→1 2 3 ⍙ ↑(1 2)(3 4 5)←→1 2 ⍙ ↑'ab'←→'a' ⍙ ↑123←→123 ⍙ ↑⍬←→0 +// ⊃(1 2 3)(4 5 6)←→1 2 3 ⍙ ⊃(1 2)(3 4 5)←→1 2 ⍙ ⊃'ab'←→'a' ⍙ ⊃123←→123 ⍙ ⊃⍬←→0 const first=x=>{let y=x.a.length?x.a[0]:getProt(x);return y.isA?y:A([y],[])} voc['⍉']=(y,x)=>{ diff --git a/examples/5-rule30.apl b/examples/5-rule30.apl index 21e80a0..c145291 100755 --- a/examples/5-rule30.apl +++ b/examples/5-rule30.apl @@ -4,4 +4,4 @@ r←30 n←50 t←⌽r⊤⍨8⍴2 -⎕←' #'[⊃⌽{⍵,⍨⊂t[2⊥¨3,/0,0,⍨↑⍵]}⍣n⊂z,1,z←n⍴0] +⎕←' #'[↑⌽{⍵,⍨⊂t[2⊥¨3,/0,0,⍨⊃⍵]}⍣n⊂z,1,z←n⍴0] diff --git a/examples/6-queens.apl b/examples/6-queens.apl index ef0d653..4472a08 100755 --- a/examples/6-queens.apl +++ b/examples/6-queens.apl @@ -5,10 +5,10 @@ queens←{ ⍝ The N-queens problem. search←{ ⍝ Search for all solutions. (⊂⍬)∊⍵:0⍴⊂⍬ ⍝ stitched: abandon this branch. 0=⍴⍵:rmdups ⍺ ⍝ all done: solution! - (hd tl)←(↑⍵)(1↓⍵) ⍝ head 'n tail of remaining ranks. + (hd tl)←(⊃⍵)(1↓⍵) ⍝ head 'n tail of remaining ranks. next←⍺∘,¨hd ⍝ possible next steps. rems←hd free¨⊂tl ⍝ unchecked squares. - ⊃,/next ∇¨rems ⍝ ... in following ranks. + ↑,/next ∇¨rems ⍝ ... in following ranks. } cvex←(1+⍳⍵)×⊂¯1 0 1 ⍝ Checking vectors. @@ -18,15 +18,15 @@ queens←{ ⍝ The N-queens problem. rmdups←{ ⍝ Ignore duplicate solution. rots←{{⍒⍵}\4/⊂⍵} ⍝ 4 rotations. refs←{{⍋⍵}\2/⊂⍵} ⍝ 2 reflections. - best←{(↑⍋⊃⍵)⊃⍵} ⍝ best (=lowest) solution. - all8←,⊃refs¨rots ⍵ ⍝ all 8 orientations. + best←{(⊃⍋↑⍵)⊃⍵} ⍝ best (=lowest) solution. + all8←,↑refs¨rots ⍵ ⍝ all 8 orientations. (⍵≡best all8)⊃⍬(,⊂⍵) ⍝ ignore if not best. } fmt←{ ⍝ Format solution. - chars←'·⍟'[(⊃⍵)∘.=⍳⍺] ⍝ char array of placed queens. - expd←1↓,⊃⍺⍴⊂0 1 ⍝ expansion mask. - ⊃¨↓↓expd\chars ⍝ vector of char matrices. + chars←'·⍟'[(↑⍵)∘.=⍳⍺] ⍝ char array of placed queens. + expd←1↓,↑⍺⍴⊂0 1 ⍝ expansion mask. + ↑¨↓↓expd\chars ⍝ vector of char matrices. } squares←(⊂⍳⌈⍵÷2),1↓⍵⍴⊂⍳⍵ ⍝ initial squares