簡體   English   中英

Haskell:無法將預期類型'(a,b0)'與實際類型'(a,b,h)'相匹配

[英]Haskell: Couldn't match expected type ‘(a, b0)’ with actual type ‘(a, b, h)’

我的代碼:

g :: (a, b, h) -> (c, d, i) -> ((a, c))
g x y = ((fst x, fst y))

為什么會顯示以下錯誤?

ax.hs:11:15: error:
    • Couldn't match expected type ‘(a, b0)’
                  with actual type ‘(a, b, h)’
    • In the first argument of ‘fst’, namely ‘x’
      In the expression: fst x
      In the expression: ((fst x, fst y))
    • Relevant bindings include
        x :: (a, b, h) (bound at ax.hs:11:3)
        g :: (a, b, h) -> (c, d, i) -> (a, c) (bound at ax.hs:11:1)
   |
11 | g x y = ((fst x, fst y))
   |               ^

ax.hs:11:22: error:
    • Couldn't match expected type ‘(c, b1)’
                  with actual type ‘(c, d, i)’
    • In the first argument of ‘fst’, namely ‘y’
      In the expression: fst y
      In the expression: ((fst x, fst y))
    • Relevant bindings include
        y :: (c, d, i) (bound at ax.hs:11:5)
        g :: (a, b, h) -> (c, d, i) -> (a, c) (bound at ax.hs:11:1)
   |
11 | g x y = ((fst x, fst y))

請幫忙。 我不確定為什么會這樣。 我如何解決它? 謝謝

fst :: (a, b) -> a僅適用於2個元組,而不適用於Arity不同於2的元組。

您可以使用模式匹配來獲取三元組的第一項,而且我個人也將模式匹配用於第二個元組,因為我認為這是一種更透明的語法:

g :: (a, b, h) -> (c, d, i) -> (a, c)
g (x, _, _) (y, _, _) = (x, y)

請注意, ((a, c))(a, c) ,因此我們可以刪除多余的括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM