Haskell元组的重复类型错误

时间:2016-03-27 20:41:25

标签: haskell

我在haskell中有这种数据类型

type Coordinate = (Int,Int)
type Skyline = [Coordinate]

我试图这样做:

combina :: (Skyline, Skyline) -> Skyline
combina ([], x) = x
combina (x, []) = x
combina ((ii, ia):ri, (di, da):rd) = subcombina(((ii, ia):ri,0), (((di, da):rd),0), 0)
    where subcombina(((ii, ai):ri,uai), (((id, ad):rd),uad), uaa)
            | ai < ad && max(uai uad) /= uaa                      = (ii, max ai uad) : subcombina((ri,ai), ((id, ad):rd, uad), max(ai uad))
            | ai < ad && max(uai uad) == uaa                      =                    subcombina((ri,ai), ((id, ad):rd, uad), uaa)
            | ((ai > ad) || (ai == ad)) && max(uai uad) /= uaa    = (id, max uai ad) : subcombina(((ii, ai):ri,uai),(rd, ad), max (uai ad))
            | ((ai > ad) || (ai == ad)) && max (uai uad) /= uaa   =                    subcombina(((ii, ai):ri,uai),(rd, ad), uaa)

所以,我试图用

调用subcombina
(((leftElementOfFirstLeftListTuple,rigthElementOfFirstLeftListTuple):restOfList,anyInteger),((leftElementOfFirstRigthListTuple,rigthElementOfFirsttRigthLListTuple):restOfList,anyInteger),
anyInteger)

我得到了这个错误:

:l Skyline                                                                                                                                                                           
[1 of 1] Compiling Skyline          ( Skyline.hs, interpreted )

Skyline.hs:26:38:
    Couldn't match type ‘t0 -> a0’ with ‘Int’
    Expected type: Skyline
      Actual type: [(Int, t0 -> a0)]
    In the expression:
      subcombina (((ii, ia) : ri, 0), (((di, da) : rd), 0), 0)
    In an equation for ‘combina’:
        combina ((ii, ia) : ri, (di, da) : rd)
          = subcombina (((ii, ia) : ri, 0), (((di, da) : rd), 0), 0)
          where
              subcombina (((ii, ai) : ri, uai), (((id, ad) : rd), uad), uaa)
                | ai < ad && max (uai uad) /= uaa
                = (ii, max ai uad)
                  : subcombina ((ri, ai), ((id, ad) : rd, uad), max (ai uad))
                | ai < ad && max (uai uad) == uaa
                = subcombina ((ri, ai), ((id, ad) : rd, uad), uaa)
                | ((ai > ad) || (ai == ad)) && max (uai uad) /= uaa
                = (id, max uai ad)
                  : subcombina (((ii, ai) : ri, uai), (rd, ad), max (uai ad))
                | ((ai > ad) || (ai == ad)) && max (uai uad) /= uaa
                = subcombina (((ii, ai) : ri, uai), (rd, ad), uaa)

Skyline.hs:26:55:
    Couldn't match expected type ‘t0 -> a0’ with actual type ‘Int’
    In the expression: ia
    In the first argument of ‘(:)’, namely ‘(ii, ia)’

Skyline.hs:26:59:
    Couldn't match type ‘Int’ with ‘t0 -> a0’
    Expected type: [(Int, t0 -> a0)]
      Actual type: [Coordinate]
    In the second argument of ‘(:)’, namely ‘ri’
    In the expression: (ii, ia) : ri

Skyline.hs:26:73:
    Couldn't match expected type ‘t0 -> a0’ with actual type ‘Int’
    In the expression: da
    In the first argument of ‘(:)’, namely ‘(di, da)’

Skyline.hs:26:77:
    Couldn't match type ‘Int’ with ‘t0 -> a0’
    Expected type: [(Int, t0 -> a0)]
      Actual type: [Coordinate]
    In the second argument of ‘(:)’, namely ‘rd’
    In the expression: ((di, da) : rd)

Skyline.hs:28:103:
    Occurs check: cannot construct the infinite type: t1 ~ t1 -> a
    Expected type: (t1 -> a) -> a
      Actual type: t1 -> a
    Relevant bindings include
      uaa :: a -> a (bound at Skyline.hs:27:62)
      uad :: t1 -> a (bound at Skyline.hs:27:56)
      rd :: [(t, t1 -> a)] (bound at Skyline.hs:27:52)
      ad :: t1 -> a (bound at Skyline.hs:27:48)
      uai :: (t1 -> a) -> a (bound at Skyline.hs:27:35)
      ri :: [(t, t1 -> a)] (bound at Skyline.hs:27:32)
      (Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-relevant-binds)
    In the expression: ai
    In the expression: (ri, ai)
Failed, modules loaded: none.

我认为我正在混合类型,但我找不到哪里。

2 个答案:

答案 0 :(得分:4)

您正尝试使用(除其他事项外)

来致电max
max(ai uad)

这表示&#34;将函数ai应用于值uad并将max应用于结果。&#34;你可能想要使用

max ai uad

答案 1 :(得分:2)

你有类似

的东西
max (ai uad) 
你的代码中的

。这适用于ai uad 但我想你想要的只是:

max ai uad