Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. signature Stack =
  2.   sig
  3.     type 'a stack
  4.     exception Empty
  5.     val empty : 'a stack
  6.     val push : 'a stack -> 'a -> 'a stack
  7.     val pop : 'a stack -> ('a * 'a stack)
  8.   end
  9.  
  10.  
  11. structure List_Stack :> Stack =
  12.   struct
  13.     type 'a stack = 'a list
  14.     exception Empty
  15.     val empty = []
  16.     fun push s a = a::s
  17.     fun pop s = case s of
  18.                   [] => raise Empty
  19.                  | x::xs => (x, xs)
  20.   end
  21.  
  22. structure Cons_Stack :> Stack =
  23.   struct
  24.     datatype 'a stack = Nil | Cons of 'a * 'a stack
  25.     exception Empty
  26.     val empty = Nil
  27.     fun push s a = Cons (a, s)
  28.     fun pop s = case s of
  29.                     Nil => raise Empty
  30.                   | Cons (x, xs) => (x, xs)
  31.   end
  32.  
  33.  
  34. signature Queue =
  35.   sig
  36.     type 'a queue
  37.     exception Empty
  38.     val empty : 'a queue
  39.     val enqueue : 'a queue -> 'a -> 'a queue
  40.     val dequeue : 'a queue -> ('a * 'a queue)
  41.   end
  42.  
  43. functor Double_Stack_Queue_Fn (myStack : Stack) :> Queue =
  44.   struct
  45.     val empty = (myStack.empty, myStack.empty)
  46.     fun enqueue (f, s) e = (push f e, s)
  47.     fun dequeue (f, s) = case s of
  48.                              [] => if f = myStack.empty then raise Empty else dequeue (empty, rev f)
  49.                            | x::xs => (x, (f, xs))
  50.   end
  51.  
  52. structure Double_List_Stack_Queue = Double_Stack_Queue_Fn (List_Stack);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement