2 none loop cis120 spring 2012 fun noa qnode option

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: no with! | None -> len! | Some n -> loop n.next (1+len)! end! Stack length q Heap fun (q:'a queue) ->! let rec loop (no:…) (len:int)=! …! in! loop q.head 0! loop no Note: we popped the old values of loop, no, len, and n when we did the tail call. Then we pushed the new values of loop, no, and len. This leaves the stack in almost the same shape as when we first called loop. Effec@vely, we have updated the stack slots for no and len. CIS120 / Spring 2012 head! tail! len 1! v! 1! next! v! 2! next! loop fun (no:'a qnode option) (len:int) -> begin match no with! | None -> len! | Some n -> loop n.next (1+len)! 59 end! ! Tail Calls and Itera@ve length Workspace begin match with! | None -> len! | Some n -> loop n.next (1+len)! end! Stack length q Heap fun (q:'a queue) ->! let rec loop (no:…) (len:int)=! …...
View Full Document

Ask a homework question - tutors are online