当我接触的F#编程越多,我用到递归的可能性就越大,也正是因为这样,我时常会遇到堆栈溢出的问题,要想避免堆栈溢出问题,Continuation Style Program(CSP)是唯一的方法。以下我们列出普通的递归和CSP的版本代码进行对比,在这里,关键的一点,该方法不会返回,因此它不会在调用的堆栈上创建元素,同时,由于会延迟计算Continuation方法,它不需要被保存在栈元素中:

View Code
1 module FunctionReturnModule =   
2    let l = [1..1000000]  
3    let rec sum l =   
4      match l with  
5      | [] -> 0  
6      | h::t -> h + sum t  
7    sum l  
View Code
 1 module CPSModule =   
 2    let l = [1..1000000]  
 3    let rec sum l cont =   
 4      match l with  
 5      | [] -> cont 0  
 6      | h::t ->   
 7        let afterSum v =   
 8          cont (h+v)  
 9        sum t afterSum  
10    sum l id 

好吧,接下来的问题是如何从普通递归的方法得到CSP的递归版本呢?以下是我遵循的步骤,记住:其中一些中间代码并不能通过编译
首先看看我们原始的递归代码:

View Code
1 module FunctionReturnModule =   
2    let l = [1..1000000]  
3    let rec sum l =   
4      match l with  
5      | [] -> 0  
6      | h::t ->   
7        let r = sum t  
8        h + r  
9    sum l

第一步:

View Code
1  module FunctionReturnModule =   
2    let l = [1..1000000]  
3    let rec sum l cont =   
4      match l with  
5      | [] -> 0  
6      | h::t ->   
7        let r = sum t  
8        cont (h + r)  
9    sum l  

第二步:处理递归函数中的sum,将cont移动到afterSum中,afterSum方法获得到参数v并将它传递给cont(h+v):

View Code
 1 module CPSModule =   
 2    let l = [1..1000000]  
 3    let rec sum l cont =   
 4      match l with  
 5      | [] -> cont 0  
 6      | h::t ->   
 7        let afterSum v =   
 8          cont (h+v)  
 9        sum t afterSum  
10    sum l id 

那么,接下来让我们使用相同的方法来遍历树,下面先列出树的定义:

View Code
1  type NodeType = int  
2  type BinaryTree =  
3    | Nil  
4    | Node of NodeType * BinaryTree * BinaryTree 

最终的结果如下:

View Code
 1 module TreeModule =   
 2    let rec sum tree =   
 3      match tree with  
 4      | Nil -> 0  
 5      | Node(v, l, r) ->  
 6        let sumL = sum l  
 7        let sumR = sum r  
 8        v + sumL + sumR  
 9    sum deepTree  
10  module TreeCSPModule =   
11    let rec sum tree cont =   
12      match tree with  
13      | Nil -> cont 0  
14      | Node(v, l, r) ->  
15        let afterLeft lValue =   
16          let afterRight rValue =   
17            cont (v+lValue+rValue)  
18          sum r afterRight  
19        sum l afterLeft  
20    sum deepTree id 

开始使用相同的步骤将它转换成CSP方式:

首先切入Continuation函数:

View Code
1 module TreeModule =   
2    let rec sum tree cont =   
3      match tree with  
4      | Nil -> 0  
5      | Node(v, l, r) ->  
6        let sumL = sum l  
7        let sumR = sum r  
8        cont (v + sumL + sumR)  
9    sum deepTree

第一步:处理sumR,将cont方法移动到afterRight中并将它传给sum r:

View Code
 1 module TreeModule =   
 2    let rec sum tree cont =   
 3      match tree with  
 4      | Nil -> 0  
 5      | Node(v, l, r) ->  
 6        let sumL = sum l  
 7        // let sumR = sum r  
 8        let afterRight rValue =    
 9          cont (v + sumL + rValue)  
10        sum r afterRight  
11    sum deepTree 

第二步:处理sumL:

View Code
 1 module TreeModule =   
 2    let rec sum tree cont =   
 3      match tree with  
 4      | Nil -> 0  
 5      | Node(v, l, r) ->  
 6        //let sumL = sum l  
 7        let afterLeft lValue =  
 8          let afterRight rValue =    
 9            cont (v + lValue + rValue)  
10          sum r afterRight  
11        sum l afterLeft  
12    sum deepTree 

结束了,接下来让我们用下面的代码进行测试吧:

View Code
1  let tree n =   
2    let mutable subTree = Node(1, Nil, Nil)  
3    for i=0 to n do  
4      subTree <- Node(1, subTree, Nil)  
5    subTree  
6  let deepTree = tree 1000000  


注:本文为译文,原文来自:http://apollo13cn.blogspot.com/2012/10/f-continuation-style-programming.html

posted on 2012-11-05 15:57  Jeallyn  阅读(2080)  评论(7编辑  收藏  举报