Очередь в списке в OCaml - PullRequest
       34

Очередь в списке в OCaml

0 голосов
/ 06 декабря 2018

Я должен реализовать абстрактный тип данных, это должен быть Queue, который соответствует этой подписи:

module type QUEUE_FUN =
sig
  (* Module [QueueFun]: first-in first-out queues *)

  (* This module implements queues (FIFOs)in a functional way. *)

  type 'a t
        (* The type of queues containing elements of type ['a]. *)
  exception Empty of string
        (* Raised when [first] is applied to an empty queue. *)
  val create: unit -> 'a t
        (* Return a new queue, initially empty. *)
  val enqueue: 'a * 'a t -> 'a t
        (* [enqueue x q] adds the element [x] at the end of queue [q]. *)
  val dequeue: 'a t -> 'a t
        (* [dequeue q] removes the first element in queue [q] *)        
  val first: 'a t -> 'a
        (* [first q] returns the first element in queue [q] without removing  
           it from the queue, or raises [Empty] if the queue is empty.*) 
  val isEmpty: 'a t -> bool
        (* [isEmpty q] returns [true] if queue [q] is empty, 
           otherwise returns [false]. *)
end;;

Это также должно быть сделано в списке, поэтому я попробовал это:

module Queue_B : QUEUE_FUN =
  struct
    type 'a t = List of 'a
    exception Empty of string
    let create() = []
    let enqueue(x, q) = [x] @ q
    let dequeue = function
        [] -> []
      | h::t -> t
    let first = function
        [] -> raise (Empty "module Queue: first")
      | h::t -> h
    let isEmpty q = q = []
  end;;

Но я не могу понять, как сделать правильный тип, и у меня есть эта ошибка:

Error: Signature mismatch:
       ...
       Values do not match:
         val create : unit -> 'a list
       is not included in
         val create : unit -> 'a t

Что мне делать тогда?Это должно быть сделано в списке.

1 Ответ

0 голосов
/ 06 декабря 2018

Хорошо, я просто попробовал:

type 'a t = 'a list

И сейчас он работает нормально

...