Y Combinatorãåãããï¼
ã£ã¦è¨ãããã£ããã©ãé£ããã¦ç解ã§ãã¾ããï¼ï¼
ã©ã ãã®è©±ããã¦ããä¼ç¤¾ã®äººããRecursive lambda expressions – The Mellow Musings of Dr. Tã®èª¬æããã¦ããã¾ããã
å»å¹´ã®ç§ã«ãã¯ãã§è¦ãã¨ãã¯é£ãããã ããã¹ã«ã¼ãããã©ããä»åã¯ç解ãããã¨é å¼µã£ã¦è¦ã¦ã¿ã¾ãã
ãã£ã±ãç解ã¯ã§ããªãorz
ã§ããããã§è«¦ãããè² ããªã®ã§ãã¨ãããã触ã£ã¦ã¿ã¾ããã
æãæ¸ãã¦ã¿ããã£ããããæ°åã§è©¦ãã¦ã¿ã
public static void Main()
{
foreach (var item in Math.Fib(10))
Console.WriteLine(item);
}
static public class Math
{
static public IEnumerable<int> Fib(int count)
{
Func<int, int> f = null;
f = i => (i < 2) ? 1 : f(i - 1) + f(i - 2);
foreach (int i in Enumerable.Range(0, count))
yield return f(i);
}
}
ããã®é¨åâ
Func<int, int> f = null;
ãã®åæåã¨åæã«å ¥ããããªãã®ããæãããªã¼ã§çµãã£ã¦ããã§ãããã
ã©ãããY Combinatorã£ã¦ãã¤ã¯ããã解æ¶ã§ãããããï¼ï¼
ãããY Combinatorãããã§ã
static Func<T, T> Fix<T>(Func<Func<T, T>, Func<T, T>> F)
{
return t => F(Fix(F))(t);
}
ã§ããããªãã¾ããã
static public IEnumerable<int> Fib(int count)
{
foreach (int ii in Enumerable.Range(0, count))
yield return Fix((Func<Func<int, int>, Func<int, int>>)(ff => i => ((i < 2) ? 1 : ff(i - 1) + ff(i - 2))))(ii);
}
ãããã£ã£ããã»ã»ï¼ãã
ã§ãã§ã使ãæ¹ãã£ã¦ãã®ãããããªãï¼
ãã£ã¹ãé·ããã©ãã©ã£ãå¥ã®ã¨ããã«å
¥ãããæ¨è«ã§ããªãã®ããªã»ã»ã»
ããã§çµãã
ã¨ããããã¯ããã以ä¸ã¯è¿½ããªãã
ãããããç´ç²ã«é¢æ°åè¨èªããã£ã¦ã¿ããããF#ããã£ã¦ã¿ã¾ã^^
é¢æ°åè¨èªã¯å帰ã¨ãç°¡åã ããï¼£ï¼ã§å¤§å¤ãªãã¨ãç°¡åã«ã§ããã®ã¯ãã£ã±ããããªã¼
let recï¼ãlet recï¼