F#中用Continuation编程避免堆栈溢出

开发 后端
今天我们将运用F#中的Continuation Style Program(CSP)方法,避免堆栈溢出问题。

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

  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 
 
  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的递归版本呢?以下是我遵循的步骤,记住:其中一些中间代码并不能通过编译。

首先看看我们原始的递归代码:

第一步:

  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 

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

  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 

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

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

最终的结果如下:

  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函数:

 
  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:

 
  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:

 
  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 

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

  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   

 

责任编辑:彭凡 来源: 博客园
相关推荐

2010-01-07 10:04:18

F#函数式编程

2011-06-09 09:52:41

F#

2010-01-26 08:25:06

F#语法F#教程

2010-08-27 09:06:49

F#

2010-01-15 08:33:13

F#F#类型推断F#教程

2009-11-09 17:51:51

F#函数式编程

2010-04-07 16:51:59

F#

2012-11-27 16:03:00

堆栈溢出

2012-11-27 15:46:51

堆栈溢出

2010-07-07 13:11:20

ScalaF#C#

2010-07-09 14:12:00

ScalaF#C#

2009-08-13 17:39:48

F#数据类型Discriminat

2009-08-19 09:42:34

F#并行排序算法

2015-03-15 01:55:25

编程语言排行榜

2009-09-10 14:18:59

Functional F#

2010-03-26 19:22:08

F#代理

2009-12-04 09:16:44

Visual Stud

2009-12-14 09:04:10

F#运算符

2009-11-16 09:05:46

CodeTimer

2009-06-22 13:43:01

F#函数式编程
点赞
收藏

51CTO技术栈公众号