Skip to content

Instantly share code, notes, and snippets.

@yvt
Last active December 27, 2015 01:29
Show Gist options
  • Save yvt/7245702 to your computer and use it in GitHub Desktop.
Save yvt/7245702 to your computer and use it in GitHub Desktop.
Q Language 不動点コンビネータ
{ }
{ 不動点コンビネータだよ〜 }
{ https://ja.wikipedia.org/wiki/不動点コンビネータ }
{ }
func Times(n: int, f: func<(int)>)
for i(1, n)
f(i)
end for
end func
func FixedPoint`[T](f: func<(func<(T):T>):func<(T):T>>): func<(T):T>
return func(x: T): T
return f(FixedPoint`[T](f))(x)
end func
end func
func AppMain()
var fac:: FixedPoint`[int](func(fac:func<(int):int>): func<(int):int>
return func(x: int): int
return (x = 0) ? (1, x * fac(x - 1))
end func
end func)
Times(20, func(i: int)
Dbg@Log(i.ToStr() ~ "! = " ~ fac(i).ToStr())
end func)
Q@Stop()
end func
{ { 出力 }
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
} { }
@yvt
Copy link
Author

yvt commented Nov 6, 2013

intは64ビット符号付き整数なのでオーバーフローしてしまいます (Q言語ではintのオーバーフローはエラー扱いされます)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment