日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

創(chuàng)新互聯(lián)為企業(yè)提供:成都品牌網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、重慶小程序開發(fā)、營銷型網(wǎng)站建設(shè)和網(wǎng)站運(yùn)營托管,一站式網(wǎng)絡(luò)營銷整體服務(wù)。實(shí)現(xiàn)不斷獲取潛在客戶之核心目標(biāo),建立了企業(yè)專屬的“全網(wǎng)整合營銷推廣”,就用不著再為了獲取潛在客戶而苦惱,相反,客戶會(huì)主動(dò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代碼并不能通過編譯。

首先看看我們?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

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

 
 
  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é)束了,接下來讓我們用下面的代碼進(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  

當(dāng)前名稱:F#中用Continuation編程避免堆棧溢出
文章網(wǎng)址:http://www.dlmjj.cn/article/dhcgpeo.html