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
Add an explicit type signature:
or better still
This happens because numbers like
7
can be any integral type, and it defaults to Integer, whereas your functions use Int.