3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Church数でfizzbuzz

Last updated at Posted at 2019-03-01

「javascriptで難読fizzbuzz」っていうお題で書き始めました。

rnという関数でChurch数をjavascriptの整数に変換していますが、これは結果の表示用であって計算は全てChurch数のまま行ってます。

実行時の効率は考えてないので、結果が表示されるまで数秒かかります。

fizzbuzz.js
const z=f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))) //Zコンビネーター
const id=x=>x //恒等関数
const sc=n=>f=>x=>f(n(f)(x)) //succ
const pr=n=>f=>x=>n(g=>h=>h(g(f)))(tr(x))(id) //pred
const mn=m=>n=>n(pr)(m) //引き算
const pl=m=>n=>n(sc)(m) //足し算
const ml=m=>n=>n(pl(m))(zr) //掛け算
const zr=f=>x=>x //0
const th=f=>x=>f(f(f(x))) //3
const fv=f=>x=>f(f(f(f(f(x))))) //5
const ft=ml(th)(fv) //15
const hd=ml(ml(fv)(fv))(sc(th)) //100
const tr=t=>f=>t //true
const fl=t=>f=>f //false
const iz=n=>n(tr(fl))(tr) //isZero
const nt=b=>b(fl)(tr) //not
const an=b=>c=>b(c(tr)(fl))(fl) //and
const or=b=>c=>nt(an(nt(b))(nt(c))) //or
const lt=z(f=>a=>b=>or(iz(a))(iz(b))(()=>nt(iz(b)))(()=>f(pr(a))(pr(b)))()) //less than
const md=z(f=>m=>n=>lt(m)(n)(()=>m)(()=>f(mn(m)(n))(n))()) //割り算のあまり
const cn=x=>y=>f=>f(x)(y) //cons

 //Church数からjavascriptの整数に変換(表示用)
const rn=z(f=>n=> iz(n)(()=>0)(()=>f(pr(n))+1)())

//Church数を受け取って、fizz, buzz, javascriptの整数のいずれかを返す関数
const fb=n=>iz(md(n)(ft))("FizzBuzz")(iz(md(n)(fv))("Buzz")(iz(md(n)(th))("Fizz")(rn(n)))) 

//100から0までの fizzbuzz の結果のリスト(降順)
const fbs=z(f=>n=>iz(n)(()=>cn(cn(zr)(zr))(zr))(()=>cn(cn(n)(fb(n)))(f(pr(n))))())(hd)

//結果のリストを逆順に出力
const ma=z(f=>ls=>iz(ls(tr)(tr))(()=>zr)(()=>(f(ls(fl)),console.log(ls(tr)(fl))))())(fbs)

以下はワンライナーver.

fizzbuzz2.js
(f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>ls=>(n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(ls(t=>f=>t)(t=>f=>t))(()=>(f=>x=>x))(()=>(f(ls(t=>f=>f)),console.log(ls(t=>f=>t)(t=>f=>f))))())((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>n=>(n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(n)(()=>(x=>y=>f=>f(x)(y))((x=>y=>f=>f(x)(y))(f=>x=>x)(f=>x=>x))(f=>x=>x))(()=>(x=>y=>f=>f(x)(y))((x=>y=>f=>f(x)(y))(n)((n=>(n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>m=>n=>((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>a=>b=>(b=>c=>b(t=>f=>t)(c))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(a))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(b))(()=>(b=>b(t=>f=>f)(t=>f=>t))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(b)))(()=>f((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(a))((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(b)))()))(m)(n)(()=>m)(()=>f((m=>n=>n(n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(m))(m)(n))(n))()))(n)((m=>n=>n((m=>n=>n(n=>f=>x=>f(n(f)(x)))(m))(m))(f=>x=>x))(f=>x=>f(f(f(x))))(f=>x=>f(f(f(f(f(x))))))))("FizzBuzz")((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>m=>n=>((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>a=>b=>(b=>c=>b(t=>f=>t)(c))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(a))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(b))(()=>(b=>b(t=>f=>f)(t=>f=>t))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(b)))(()=>f((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(a))((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(b)))()))(m)(n)(()=>m)(()=>f((m=>n=>n(n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(m))(m)(n))(n))()))(n)(f=>x=>f(f(f(f(f(x)))))))("Buzz")((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>m=>n=>((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>a=>b=>(b=>c=>b(t=>f=>t)(c))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(a))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(b))(()=>(b=>b(t=>f=>f)(t=>f=>t))((n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(b)))(()=>f((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(a))((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(b)))()))(m)(n)(()=>m)(()=>f((m=>n=>n(n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(m))(m)(n))(n))()))(n)(f=>x=>f(f(f(x)))))("Fizz")(((f=>(x=>f(y=>x(x)(y)))(x=>f(y=>x(x)(y))))(f=>n=> (n=>n((t=>f=>t)(t=>f=>f))(t=>f=>t))(n)(()=>0)(()=>f((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(n))+1)()))(n)))))(n)))(f((n=>f=>x=>n(g=>h=>h(g(f)))((t=>f=>t)(x))(x=>x))(n))))())((m=>n=>n((m=>n=>n(n=>f=>x=>f(n(f)(x)))(m))(m))(f=>x=>x))((m=>n=>n((m=>n=>n(n=>f=>x=>f(n(f)(x)))(m))(m))(f=>x=>x))(f=>x=>f(f(f(f(f(x))))))(f=>x=>f(f(f(f(f(x)))))))((n=>f=>x=>f(n(f)(x)))(f=>x=>f(f(f(x)))))))

ワンライン(2571文字)

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?