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 func@on: 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! end! in! loop q.head! •  This code for length uses a helper func@on, 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 propor@onal to the length of the queue –  That seems inefficient… why should it take so much space? CIS120 / Spring 2012 11 Evalua@ng...
View Full Document

This document was uploaded on 11/24/2013.

Ask a homework question - tutors are online