-- Ring as an ADT module Ring ( Ring, Elt, newRing, current, deleteCurrent, rotate, size) where ---------------------------------------- -- Interface ---------------------------------------- newRing :: [Elt] -> Ring current :: Ring -> Elt deleteCurrent :: Ring -> Ring -- after deleting current is the -- element following the deleted elt rotate :: Ring -> Int -> Ring size :: Ring -> Int ---------------------------------------- -- Implementation ---------------------------------------- ---------------------- -- type implementation ---------------------- type Elt = Int data Ring = Gandalf [Elt] deriving (Show) -- just for debugging -------------------------- -- Function implementation -------------------------- newRing elts = (Gandalf elts) current (Gandalf (first:_)) = first deleteCurrent (Gandalf (first:rest)) = (Gandalf rest) rotate (Gandalf elts) amount = Gandalf (newFront++newRear) where modAmount = amount `mod` ringSize ringSize = length elts newFront = drop amount elts newRear = take amount elts size (Gandalf elts) = length elts