错误:此表达式具有类型缓存列表,但此处与类型缓存列表一起使用

时间:2016-04-06 00:12:55

标签: list ocaml

当我在Ocaml终端上呼叫时,我的程序出现问题" #matrix81 cache ;;"它给了我错误:"这个表达式有类型缓存列表,但在这里用于类型缓存列表" 这是我的代码。有什么帮助吗?

  let rec makeLine w = 
    let y = w - 1 in
    if w <> 0 then 0::(makeLine y)
    else []

;; 


    let rec makeMatrix w h = 
        let y = h - 1 in
        if h <> 0 then (makeLine w)::(makeMatrix w y)
        else []
    ;;

    let rec checkCache lc d t = 
        match lc with
        [] -> 0
        |x::xs -> if (x.difficulty = d) && (x.terrain = t) then (checkCache xs d t) + 1
              else (checkCache xs d t)

    ;;

    let rec checkLine lc d t line =
        match line with 
        []->[]
        |x::xs -> let nt = t +. 0.5 in
              let v = 5.0 in
              if (nt < v) then
              let nx = (checkCache lc d t) in
              (nx)::(checkLine lc d nt xs)
              else []
    ;;


    let rec matrix81Aux m d lc = 
        match m with
        [] -> []
        |x::xs -> let nd = d +. 0.5 in
              let v = 5.0 in
              if (nd < v) then
              (checkLine lc d 1.0 x)::(matrix81Aux xs nd lc)
              else []
    ;;


    let matrix81 lc = 
        let m = makeMatrix 9 9 in
        matrix81Aux m 1.0 lc
    ;;

1 个答案:

答案 0 :(得分:1)

您没有显示类型cache的定义(或给出错误的行号)。

这种奇怪类型的错误消息的最常见原因是您已经两次定义了相同的类型名称。当从顶层工作并使用#use加载文件时,通常会发生这种情况。

您也可能以其他方式两次定义名称cache

最近版本的OCaml在类型名称中添加了一个整数,以(试图)澄清涉及两种不同的类型:

# type cache = A | B;;
type cache = A | B
# let f = function A -> 3 | B -> 4;;
val f : cache -> int = <fun>
# type cache = C | D;;
type cache = C | D
# let g x = match x with C -> f x | D -> 14;;
Error: This expression has type cache/1023
       but an expression was expected of type cache/1018