{-<br _moz_dirty="" />Below is a *specification* of a queue. <br _moz_dirty="" />If possible I would like to write the equations in type class.<br _moz_dirty="" />Does the type class need two type variables? <br _moz_dirty="" />How do I represent the constructors?<br _moz_dirty="" />Can the equations be written in the type class rather than the instance?<br _moz_dirty="" />-}<br _moz_dirty="" /><br _moz_dirty="" />module QUEUE_SPEC where<br _moz_dirty="" />data Queue e = New | Insert (Queue e) e deriving Show<br _moz_dirty="" /><br _moz_dirty="" />isEmpty :: Queue e -> Bool<br _moz_dirty="" />isEmpty New = True <br _moz_dirty="" />isEmpty (Insert q e) = False <br _moz_dirty="" /><br _moz_dirty="" />first :: Queue e -> e<br _moz_dirty="" />first (Insert q e) = if (isEmpty q) then e else (first q) <br _moz_dirty="" /><br _moz_dirty="" /><br _moz_dirty="" />rest :: Queue e -> Queue e<br _moz_dirty="" />rest (Insert q e ) = if (isEmpty q) then New else (Insert (rest q) e)<br _moz_dirty="" /><br _moz_dirty="" /><br _moz_dirty="" />size :: Queue e -> Int<br _moz_dirty="" />size New = 0 <br _moz_dirty="" />size (Insert q e) = succ (size q)<br _moz_dirty="" /><br _moz_dirty="" />{- <br _moz_dirty="" />some tests of above code<br _moz_dirty="" />size (Insert (Insert (Insert New 5) 6) 3)<br _moz_dirty="" />rest (Insert (Insert (Insert New 5) 6) 3)<br _moz_dirty="" /><br _moz_dirty="" />My first stab at a class<br _moz_dirty="" />class QUEUE_SPEC q e where<br _moz_dirty="" /> new :: q e<br _moz_dirty="" /> insert :: q e -> q e<br _moz_dirty="" /> isEmpty :: q e -> Bool<br _moz_dirty="" /> first :: q e -> e<br _moz_dirty="" /> rest :: q e -> q e<br _moz_dirty="" /> size :: q e -> Int<br _moz_dirty="" /><br _moz_dirty="" />-}<br _moz_dirty="" /><br _moz_dirty="" type="_moz" />
<br clear=all> Tá an teachtaireacht seo scanta ó thaobh ábhar agus víreas ag Seirbhís Scanta Ríomhphost de chuid Seirbhísí Faisnéise, ITBÁC agus meastar í a bheith slán. <a href="http://www.dit.ie">http://www.dit.ie</a><br>
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. <a href="http://www.dit.ie">http://www.dit.ie</a>