Stack length q heap fun qa queue let rec loop 2

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The end result is that we have turned recursion into a true loop. (Just like a ‘while’ or ‘for’ loop in Java or C.) CIS120 / Spring 2012 43 Tail Calls and Itera@ve length Workspace length q! Stack length q Bindings above this line are top- level declara@ons. Heap fun (q:'a queue) ->! let rec loop (no:…) (len:int)=! …! in! loop q.head 0! head! tail! v! 1! next! v! 2! next! CIS120 / Spring 2012 44 Tail Calls and Itera@ve length Workspace ( )! Stack length q Heap fun (q:'a queue) ->! let rec loop (no:…) (len:int)=! …! in! loop q.head 0! head! tail! v! 1! next! v! 2! next! CIS120 / Spring 2012 45 Tail Calls and Itera@ve length Workspace let rec loop (no:'a qnode option) (len:int) : int =! begin match no with! | None -> len! | Some n -> loop n.next (1+len)! end! in! loop q.head 0! Stack length q Heap fun (q:'a queue) ->! le...
View Full Document

This document was uploaded on 11/24/2013.

Ask a homework question - tutors are online