OCaml - 未绑定类型构造函数

时间:2013-12-06 05:56:34

标签: types module ocaml type-constructor

我是OCaml的新手,对Ocaml中的Module系统感到困惑

module type Queue = 
sig 
  type element
  type queue
  val enq: queue * element -> queue
end

module StringQ : Queue with type element = string =                                                                                                                          
struct                                                                                                                                                                       
  type element = string         (* If I remove this it also doesn't work *)                                                                                                                                                  
  type queue = element list                                                                                                                                                  
  exception EMPTY_Q                                                                                                                                                          
  let emptyq = []                                                                                                                                                            

  let enq: queue * element -> queue =                                                                                                                                        
    fun arg ->                                                                                                                                                               
      let (q, elem) = arg in                                                                                                                                                 
        elem @ [q]                                                                                                                                                           

  let rec deq: queue -> element * queue =                                                                                                                                    
    fun q ->                                                                                                                                                                 
      match q with                                                                                                                                                           
      | [] -> raise EMPTY_Q                                                                                                                                                  
      | x :: xs -> (x, xs)                                                                                                                                                   

end                                

我收到编译错误:This expression has type element list, but an expression was expected of type 'a list

与此同时,我将非常感谢有关Ocaml模块的任何链接。

1 个答案:

答案 0 :(得分:1)

只需更改

elem @ [q]

q @ [elem]

它为我编译(OCaml 4.00.1)。

(你实际上看起来并不困惑,这段代码教给我关于模块的东西。)