自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

F#中用Continuation編程避免堆棧溢出

開發(fā) 后端
今天我們將運(yùn)用F#中的Continuation Style Program(CSP)方法,避免堆棧溢出問(wèn)題。

當(dāng)我接觸的F#編程越多,我用到遞歸的可能性就越大,也正是因?yàn)檫@樣,我時(shí)常會(huì)遇到堆棧溢出的問(wèn)題,要想避免堆棧溢出問(wèn)題,Continuation Style Program(CSP)是唯一的方法。以下我們列出普通的遞歸和CSP的版本代碼進(jìn)行對(duì)比,在這里,關(guān)鍵的一點(diǎn),該方法不會(huì)返回,因此它不會(huì)在調(diào)用的堆棧上創(chuàng)建元素,同時(shí),由于會(huì)延遲計(jì)算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 

好吧,接下來(lái)的問(wèn)題是如何從普通遞歸的方法得到CSP的遞歸版本呢?以下是我遵循的步驟,記住:其中一些中間代碼并不能通過(guò)編譯。

首先看看我們?cè)嫉倪f歸代碼:

第一步:

  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 

第二步:處理遞歸函數(shù)中的sum,將cont移動(dòng)到afterSum中,afterSum方法獲得到參數(shù)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 

那么,接下來(lái)讓我們使用相同的方法來(lái)遍歷樹,下面先列出樹的定義:

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

最終的結(jié)果如下:

  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 

開始使用相同的步驟將它轉(zhuǎn)換成CSP方式:

首先切入Continuation函數(shù):

 
  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方法移動(dòng)到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 

結(jié)束了,接下來(lái)讓我們用下面的代碼進(jìn)行測(cè)試吧:

  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   

 

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2010-01-07 10:04:18

F#函數(shù)式編程

2011-06-09 09:52:41

F#

2010-01-26 08:25:06

F#語(yǔ)法F#教程

2010-08-27 09:06:49

F#

2010-01-15 08:33:13

F#F#類型推斷F#教程

2009-11-09 17:51:51

F#函數(shù)式編程

2010-04-07 16:51:59

F#

2010-07-07 13:11:20

ScalaF#C#

2010-07-09 14:12:00

ScalaF#C#

2012-11-27 15:46:51

堆棧溢出

2012-11-27 16:03:00

堆棧溢出

2009-08-13 17:39:48

F#數(shù)據(jù)類型Discriminat

2009-08-19 09:42:34

F#并行排序算法

2009-09-10 14:18:59

Functional F#

2015-03-15 01:55:25

編程語(yǔ)言排行榜

2009-12-04 09:16:44

Visual Stud

2009-11-16 09:05:46

CodeTimer

2010-03-26 19:22:08

F#代理

2009-12-14 09:04:10

F#運(yùn)算符

2009-06-22 13:43:01

F#函數(shù)式編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)