{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Stack length q heap fun qa queue let rec loop

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 interface with a length [email protected]: module type QUEUE = ! sig! (* type of the data structure *)! type 'a queue! …! (* Get the length of the queue *)! val length : 'a queue -> int! end! •  How can we implement it? CIS120 / Spring 2012 10 length (recursively) (* Calculate the length of the list using recursion *)! let length (q:'a queue) : int =! let rec loop (no: 'a qnode option) : int =! begin match no with! | None -> 0! | Some n -> 1 + (loop n.next)! end! in! loop q.head! •  This code for length uses a helper [email protected], loop: –  the correctness depends crucially on the queue invariant –  what happens if we pass in a bogus q that is cyclic? •  The height of the ASM stack is [email protected] to the length of the queue –  That seems inefficient… why should it take so much space? CIS120 / Spring 2012 11 [email protected]
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online