【问题标题】:Error: This expression has type cache list but is here used with type cache list错误:此表达式具有缓存列表类型,但此处与缓存列表类型一起使用
【发布时间】:2016-05-06 02:52:46
【问题描述】:

当我在 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
    ;;

【问题讨论】:

  • 请不要破坏您的帖子。请注意,一旦您在本网站上发布了问题或答案,这些帖子就会成为也为该内容做出贡献的其他人的集体努力的一部分。除非在特殊情况下,否则不应删除可能对其他人有用的帖子。即使该帖子对原作者不再有用,该信息仍然对将来可能遇到类似问题的其他人有益 - 这是 Stack Exchange 的基本理念。

标签: list ocaml


【解决方案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

【讨论】:

  • 对不起,缓存类型为:type cache = { (* Example: *) code: string;名称:字符串;状态:字符串;所有者:字符串;纬度:浮动;经度:浮动;种类:字符串;大小:字符串;难度:浮动;地形:浮动;状态:字符串;隐藏日期:字符串; nFounds: int; nNotFounds:整数; n收藏夹:int;高度:int } ;;
  • 而Ocaml在列表缓存下划线,说那是错误
猜你喜欢
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 2022-07-06
相关资源
最近更新 更多