就像第一个Scheme的程序,如果调用的是(f1.11a 28)而不是(f1.11a 512),则在4,5秒之后就出了结果,而c#的程序,即使是调用 System.Console.WriteLine(
这就是我的问题,第一个Scheme的例子和第二个c#的例子,我都特意写了不是尾递归的版本,所以不是应该按照普通的函数调用来执行么?
那么Scheme的例子,每次运行递归调用都会保留现场,如果是保留到栈里,为什么会是在执行了几分钟之后报内存耗尽,而不是像c#的例子在运行之后一瞬间报栈溢出?
抛开尾递归不说。
其实你耐心的继续看SICP,看到求值器的环境模型那一章就明白了,
FP语言的模型和procedural语言的模型是不同的,后者assume用户(也就是程序员)不会大量使用递归。故而使用栈来简化编译器实现。
我记得当年在DOS上使用Turbo Pascal时,语言的手册里明确标明,递归的使用不得超过7层,否则会发生栈溢出。
我再给一个类比问题,很经典。相信你一看就明白了。
这就是深度优先搜索迷宫问题,我们知道深度优先的最简单实现是递归实现。并且这个递归不是尾递归。
在DOS时代,迷宫的深度造成可以轻易大于7,故而递归一定会溢出,那时候竞赛遇到这类题目我们的典型解法就是使用stack。
每次搜索扩展时把当前走法压栈,然后回溯的时候弹出。
还有一个名题是八皇后,8正好比7大一,所以如果使用DFS解八皇后,就必须使用自定义的stack。
最后我再强调一点,许多多范式语言最近也争先恐后的加入了FP的一些库,这也算是感时髦吧。但是他们背后的计算模型是procedural的。
所以你可以轻易判断这些FP工具是否可以支持你来实现工业级别的应用,这个方法就是实现一个尾递归的阶乘:
例如python:
# I tested that python doesn't support tail recursion.
# as below:
# def fact(n):
# if(n==0):
# return 1
# else:
# return n*fact(n-1)
# if we call fact(1000), the error is:
# RuntimeError: maximum recursion depth exceeded
虽然python里有map, reduce, filter等等看似functional的东西,但是它用的不是FP模型。
--
LIU
https://sites.google.com/site/algoxy/home
On Feb 12, 10:51 am, 石奇偲 <shiqi...@gmail.com> wrote:
> 按照"如果某人写个Scheme解释器,
> 函数调用时不用栈,所有数据往堆上搞,自然不会栈溢出"的思路,
> c#的编译器为什么不把函数调用的现场保存在堆里?
> 至少在栈快溢出的时候把现场的保存转到堆里,这样的话如果我写的不是一个无限的递归而是一个比较深的递归,可能可以跑出结果,而不是在程序刚一运行的时候就报栈空间溢出?
>
> 在 2011年2月12日 上午10:48,Jeffrey Zhao <je...@live.com>写道:
>
> > 听不懂了,你没写成尾递归,C#怎么做优化?你的Scheme版本也没有优化啊,还是会内存不足。
>
> > Jeffrey Zhao
> > Blog:http://blog.zhaojie.me/
> > Twitter:http://twitter.com/jeffz_cn
>
> > *From:* 石奇偲 <shiqi...@gmail.com>
> > *Sent:* Saturday, February 12, 2011 10:45 AM
> > *To:* pon...@googlegroups.com
> > *Subject:* Re: [TL] 函数调用的时候,现场一定要保存在操作系统提供的栈空间里面么?
��Ȼ��ȷPython��һ�����β�ݹ��Ż��������������ȷ����β�ݹ顣
��������Ϊ������ģ����һ�֣�ִ�л�������һ�֡�Ϊ�˱�֤���ܵ����أ�û�취˭�÷�ŵ������ǹ��ʽ�ģ���FP����Ҳ���п�������ջ�ġ����Ի���Ҫ�����ס��ض����ԡ��Ĵ��?ʽ���ú���ʽ�����ʽ���л�������̫���ˡ���
Jeffrey Zhao
Blog: http://blog.zhaojie.me/
Twitter: http://twitter.com/jeffz_cn
-----ԭʼ�ʼ�-----
From: Larry, LIU Xinyu
Sent: Saturday, February 12, 2011 11:19 AM
To: TopLanguage
Subject: [TL] Re: ������õ�ʱ��,�ֳ�һ��Ҫ�����ڲ���ϵͳ�ṩ��ջ�ռ�����ô?
Hi,
��β�ݹ鲻˵��
��ʵ�����ĵļ���SICP��������ֵ���Ļ���ģ����һ�¾������ˣ�
FP���Ե�ģ�ͺ�procedural���Ե�ģ���Dz�ͬ�ģ�����assume�û���Ҳ���dz���Ա���������ʹ�õݹ顣�ʶ�ʹ��ջ��������ʵ�֡�
�Ҽǵõ�����DOS��ʹ��Turbo Pascalʱ�����Ե��ֲ�����ȷ�������ݹ��ʹ�ò��ó���7�㣬����ᷢ��ջ�����
���ٸ�һ��������⣬�ܾ��䡣������һ���������ˡ�
�����������������Թ����⣬����֪��������ȵ����ʵ���ǵݹ�ʵ�֡���������ݹ鲻��β�ݹ顣
��DOSʱ���Թ��������ɿ������״���7���ʶ�ݹ�һ�����������ʱ��������������Ŀ���ǵĵ��ͽⷨ����ʹ��stack��
ÿ��������չʱ�ѵ�ǰ�߷�ѹջ��Ȼ����ݵ�ʱ����
����һ�������ǰ˻ʺ�8��ñ�7��һ���������ʹ��DFS��˻ʺͱ���ʹ���Զ����stack��
�������ǿ��һ�㣬���ʽ�������Ҳ���ȿֺ�ļ�����FP��һЩ�⣬��Ҳ���Ǹ�ʱ�ְɡ��������DZ���ļ���ģ����procedural�ġ�
��������������ж���ЩFP�����Ƿ����֧������ʵ�ֹ�ҵ�����Ӧ�ã������������ʵ��һ��β�ݹ�Ľ׳ˣ�
����python:
# I tested that python doesn't support tail recursion.
# as below:
# def fact(n):
# if(n==0):
# return 1
# else:
# return n*fact(n-1)
# if we call fact(1000), the error is:
# RuntimeError: maximum recursion depth exceeded
��Ȼpython����map, reduce, filter�ȵȿ���functional�Ķ������������õIJ���FPģ�͡�
--
LIU
https://sites.google.com/site/algoxy/home
On Feb 12, 10:51 am, ʯ��� <shiqi...@gmail.com> wrote:
> ����"���ij���Scheme��������
> �������ʱ����ջ�������������ϸ㣬��Ȼ����ջ���"��˼·,
> c#�ı�����Ϊʲô���Ѻ�����õ��ֳ������ڶ���?
> ������ջ�������ʱ����ֳ��ı���ת������,����Ļ������д�IJ���һ�����ĵݹ����һ���Ƚ���ĵݹ�,���ܿ����ܳ����,�����ڳ����һ���е�ʱ��ͱ�ջ�ռ����?
>
> �� 2011��2��12�� ����10:48��Jeffrey Zhao <je...@live.com>���
>
> > ���ˣ���ûд��β�ݹ飬C#��ô���Ż������Scheme�汾Ҳû���Ż��������ǻ��ڴ治�㡣
>
> > Jeffrey Zhao
> > Blog:http://blog.zhaojie.me/
> > Twitter:http://twitter.com/jeffz_cn
>
> > *From:* ʯ��� <shiqi...@gmail.com>
> > *Sent:* Saturday, February 12, 2011 10:45 AM
> > *To:* pon...@googlegroups.com
> > *Subject:* Re: [TL] ������õ�ʱ��,�ֳ�һ��Ҫ�����ڲ���ϵͳ�ṩ��ջ�ռ�����ô?
>
> > ��,��Ҳ���ҵĿ���.����Ϊʲô��ô��Ȼ���Ż�,c#����?
>
> > �� 2011��2��12�� ����10:04��Jeffrey Zhao <je...@live.com>���
>
> >> �������C#��������Ͳ���β�ݹ飬ִ��ʱ���Ҫ���ϱ���ռ�ռ��ˡ�����DrScheme��˭֪��������ôִ�е��ء����ij��д��Scheme���������������ʱ����ջ�������������ϸ㣬��Ȼ����ջ��������ǻ��ڴ��þ��ˡ�
>
> >> ���������β�ݹ��Ż��ģ������ջ��������Ҳ�������¿ռ�ȵȣ���������ѭ��������ռ���ڴ档
首先,堆内存的维护者是用户,谁申请谁释放。栈内存的维护者是系统。
2011/2/12 qiaojie <qia...@gmail.com>:
The _resetstkoflw function recovers from a stack overflow condition, allowing a program to continue instead of failing with a fatal exception error. If the _resetstkoflw function is not called, there are no guard page after the previous exception. The next time that there is a stack overflow, there are no exception at all and the process terminates without warning.
If a thread in an application causes an EXCEPTION_STACK_OVERFLOW exception, the thread has left its stack in a damaged state. This is in contrast to other exceptions such as EXCEPTION_ACCESS_VIOLATION or EXCEPTION_INT_DIVIDE_BY_ZERO, where the stack is not damaged. The stack is set to an arbitrarily small value when the program is first loaded. The stack then grows on demand to meet the needs of the thread. This isI can't read the content because of wrong encoding. Do you mean the
fact function can compute 1000! in your machine?
I am currently using Python 2.5, it report this error at least in my
PC.
BTW: the python version of reduce, map, and filter internally use
iterative implementation so that they seem can recurs over big
numbers. For instance:
>>> reduce(lambda x, y: x+y, range(100000))
4999950000L
see Python-2.6.5\Modules\_functoolsmodule.c line 12
--
LIU
https://sites.google.com/site/algoxy/home
On Feb 12, 11:40 am, "Jeffrey Zhao" <je...@live.com> wrote:
> def fact(n):
> if(n==0):
> return 1
> else:
> return n*fact(n-1)
>
> Ȼ ȷPython һ β ݹ Ż ȷ β ݹ顣
>
> Ϊ ģ һ ֣ ִ л һ ֡ Ϊ ˱ ֤ ܵ أ û 취˭ ÷ ŵ ǹ ʽ ģFP Ҳ п ջ ġ Ի Ҫ ס ض ԡ Ĵ ?ʽ ú ʽ ʽ л̫ ˡ
>
> Jeffrey Zhao
> Blog:http://blog.zhaojie.me/
> Twitter:http://twitter.com/jeffz_cn
> -----ԭʼ ʼ -----
> From: Larry, LIU Xinyu
> Sent: Saturday, February 12, 2011 11:19 AM
> To: TopLanguage
> Subject: [TL] Re: õ ʱ , ֳ һ Ҫ ڲ ϵͳ ṩ ջ ռ ô?
>
> Hi,
>
> β ݹ鲻˵
>
> ʵ ĵļ SICP ֵ Ļ ģ һ ¾ ˣ
>
> FP Ե ģ ͺ procedural Ե ģ Dz ͬ ģ assume û Ҳ dz Ա ʹ õݹ顣ʶ ʹ ջ ʵ ֡
> Ҽǵõ DOS ʹ Turbo Pascalʱ Ե ֲ ȷ ݹ ʹ ò ó 7 㣬 ᷢ ջ
>
> ٸ һ ⣬ ܾ 䡣 һ ˡ
>
> Թ ⣬ ֪ ȵ ʵ ǵݹ ʵ ֡ ݹ鲻 β ݹ顣
> DOSʱ Թ ɿ ״ 7 ʶ ݹ һ ʱ Ŀ ǵĵ ͽⷨʹ stack
> ÿ չʱ ѵ ǰ ߷ ѹջ Ȼ ݵ ʱ
>
> һ ǰ˻ʺ 8 ñ 7 һ ʹ DFS ˻ʺͱ ʹ Զ stack
>
> ǿ һ 㣬 ʽ Ҳ ȿֺ ļ FP һЩ ⣬ Ҳ Ǹ ʱ ְɡ DZ ļ ģprocedural ġ
> ж ЩFP Ƿ ֧ ʵ ֹ ҵ Ӧ ã ʵ һ β ݹ Ľ׳ˣ
> python:
> # I tested that python doesn't support tail recursion.
> # as below:
> # def fact(n):
> # if(n==0):
> # return 1
> # else:
> # return n*fact(n-1)
> # if we call fact(1000), the error is:
> # RuntimeError: maximum recursion depth exceeded
>
> Ȼpython map, reduce, filter ȵȿ functional Ķ õIJ FPģ ͡
>
> --
> LIUhttps://sites.google.com/site/algoxy/home
>
> On Feb 12, 10:51 am, ʯ <shiqi...@gmail.com> wrote:
>
> > " ij д Scheme
> > ʱ ջ ϸ㣬 Ȼ ջ " ˼·,
> > c# ı Ϊʲô Ѻ õ ֳ ڶ ?
> > ջ ʱ ֳ ı ת , Ļ д IJ һ ĵݹ һ Ƚ ĵݹ, ܿ ܳ , ڳ һ е ʱ ͱ ջ ռ ?
>
> > 2011 2 12 10:48 Jeffrey Zhao <je...@live.com>д
>
> > > ˣ ûд β ݹ飬C# ô Ż Scheme 汾Ҳû Ż ǻ ڴ治 㡣
>
> > > Jeffrey Zhao
> > > Blog:http://blog.zhaojie.me/
> > > Twitter:http://twitter.com/jeffz_cn
>
> > > *From:* ʯ <shiqi...@gmail.com>
> > > *Sent:* Saturday, February 12, 2011 10:45 AM
> > > *To:* pon...@googlegroups.com
> > > *Subject:* Re: [TL] õ ʱ , ֳ һ Ҫ ڲ ϵͳ ṩ ջ ռ ô?
>
> > > , Ҳ ҵĿ . Ϊʲô ô Ȼ Ż ,c# ?
>
> > > 2011 2 12 10:04 Jeffrey Zhao <je...@live.com>д
>
> > >> C# Ͳ β ݹ飬ִ ʱ Ҫ ϱ ռ ռ ˡ DrScheme ˭֪ ôִ еء ij д Scheme ʱ ջ ϸ㣬 Ȼ ջ ǻ ڴþ ˡ
def fact(n):
if(n==0):
return 1
else:
return n*fact(n-1)
Jeffrey Zhao
Blog: http://blog.zhaojie.me/
Twitter: http://twitter.com/jeffz_cn
-----原始邮件-----
From: Larry, LIU Xinyu
Sent: Saturday, February 12, 2011 2:00 PM
To: TopLanguage
Subject: [TL] Re: 函数调用的时候,现场一定要保存在操作系统提供的栈空间里面么?
What about this one?
def fact(n, acc = 1):
if n == 0:
return acc
else:
return fact(n-1, acc*n)
>>> fact(5)
120
>>> fact(1000)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
fact(1000)
.....
RuntimeError: maximum recursion depth exceeded.
Cheers.
--
LIU
https://sites.google.com/site/algoxy/home
真把函数调用的现场保存在堆里你又要抱怨C#性能太差了
Jeffrey Zhao
Blog: http://blog.zhaojie.me/
Twitter: http://twitter.com/jeffz_cn
-----原始邮件-----
From: Larry, LIU Xinyu
Sent: Saturday, February 12, 2011 3:10 PM