无法将预期类型Int与推断类型Integer匹配

时间:2012-10-28 22:15:35

标签: haskell inferred-type

我编写了一些简单的haskell函数来计算图中给定顶点的邻居(见下文)。它编译得很好但是,当我运行adj g 1时,我收到以下错误:Couldn't match expected type `Int' against inferred type `Integer'

代码:

module Test where
import Prelude
import Data.List


type Node = Int
type Edge = (Int, Int)

type Graph = ([Node], [Edge])

g = ([1,2,3,4,5,6], [(1,2),(2,3),(2,4),(5,6)])


adj :: Graph -> Node -> [Node]
adj (vs, []) n = []
adj (vs,((s,e):es)) n   | s==n = e:rec
                        | e==n = s:rec
                        | otherwise = rec
    where
    rec = adj (vs,es) n 

1 个答案:

答案 0 :(得分:5)

添加显式类型签名:

g :: ([Int], [(Int, Int)])

或更好

g :: Graph

这是因为像7这样的数字可以是任何整数类型,它默认为Integer,而你的函数使用Int。

相关问题