Error while using indices and elem

Hi, Do I need to define indices and elem for Graph a w here explicitly ? import Data.Array data Graph a w = Array a [(a,w)] -- array (1,3) [(1,[(2,3),(3,2)])] --Given a vertex list out adjacents getAdjVertices :: (Ix a, Eq a) => a -> (Graph a w) -> [a] getAdjVertices x arr | not (x `elem` (indices arr)) = error "No such Vertex" | otherwise = extractV (arr ! 2) where extractV [] = [] extractV (x:xs) = (fst x):extractV (xs) GHCI gives : Prelude> :l programs\graph.hs [1 of 1] Compiling Main ( programs\graph.hs, interpreted ) programs\graph.hs:16:47: Couldn't match expected type `Array a e0' with actual type `Graph a w' In the first argument of `indices', namely `arr' In the second argument of `elem', namely `(indices arr)' In the first argument of `not', namely `(x `elem` (indices arr))' programs\graph.hs:17:46: Couldn't match expected type `Array Integer [(a, b0)]' with actual type `Graph a w' In the first argument of `(!)', namely `arr' In the first argument of `extractV', namely `(arr ! 2)' In the expression: extractV (arr ! 2) Failed, modules loaded: none. Prelude> Regards. Nishant

Hello Nishant, It's not clear whether you're implementing graph functionality as a device for learning Haskell, or whether you need it as a kind of a library to support future programs. If it's the latter, I want to point you to the Data.Graph module in the containers package: http://hackage.haskell.org/package/containers-0.2.0.1/docs/Data-Graph.html

On Mon, Apr 21, 2014 at 10:06 AM, Nishant
Hi,
Do I need to define indices and elem for Graph a w here explicitly ?
import Data.Array
data Graph a w = Array a [(a,w)] -- array (1,3) [(1,[(2,3),(3,2)])]
You probably meant to make "Graph a w" a type synonym but you used "data" thus it is a type constructor with one data constructor named Array which takes two parameters : an "a" and a list of pairs of "a" and "w". You wanted to write :
type Graph a w = Array a [(a,w)]
which should work better (indices will work on it like on an Array from Data.Array). -- Jedaï
participants (3)
-
Chaddaï Fouché
-
Dan Serban
-
Nishant