Couldn't match expected type Int against inferred type Integer

497 Views Asked by At

I have written some simple haskell function to compute the neighbours of a given vertex in a graph (see below). It compiles fine, however, when I run adj g 1, I get the following error: Couldn't match expected type `Int' against inferred type `Integer'

The code:

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

There are 1 best solutions below

5
On BEST ANSWER

Add an explicit type signature:

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

or better still

g :: Graph

This happens because numbers like 7 can be any integral type, and it defaults to Integer, whereas your functions use Int.