日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
F#中用Continuation編程避免堆棧溢出

當我接觸的F#編程越多,我用到遞歸的可能性就越大,也正是因為這樣,我時常會遇到堆棧溢出的問題,要想避免堆棧溢出問題,Continuation Style Program(CSP)是唯一的方法。以下我們列出普通的遞歸和CSP的版本代碼進行對比,在這里,關(guān)鍵的一點,該方法不會返回,因此它不會在調(diào)用的堆棧上創(chuàng)建元素,同時,由于會延遲計算Continuation方法,它不需要被保存在棧元素中:

創(chuàng)新互聯(lián)為企業(yè)提供:成都品牌網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、重慶小程序開發(fā)、營銷型網(wǎng)站建設(shè)和網(wǎng)站運營托管,一站式網(wǎng)絡(luò)營銷整體服務(wù)。實現(xiàn)不斷獲取潛在客戶之核心目標,建立了企業(yè)專屬的“全網(wǎng)整合營銷推廣”,就用不著再為了獲取潛在客戶而苦惱,相反,客戶會主動找您,生意就找上門來了!

 
 
  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的遞歸版本呢?以下是我遵循的步驟,記?。浩渲幸恍┲虚g代碼并不能通過編譯。

首先看看我們原始的遞歸代碼:

第一步:

 
 
  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移動到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

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

 
 
  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方法移動到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é)束了,接下來讓我們用下面的代碼進行測試吧:

 
 
  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  

當前標題:F#中用Continuation編程避免堆棧溢出
文章URL:http://m.5511xx.com/article/dhcgpeo.html