Simon H
Simon H

Reputation: 21005

Haskell: Couldn't match expected type error

I'm trying to do the Coursera algorithms course and learn Haskell at the same time, but I've got stuck now for a few hours trying to compile my work in progress. I've tried the emulate most of this in GHCi and that works, but I keep getting a type error. Would welcome an experienced eye

   Couldn't match expected type `Int -> [Int] -> Bool'
                with actual type `Int'
    The operator `elem' takes two arguments,
    but its type `Int' has none
    In the expression: otherNode `elem` v''
    In the expression: \ (GraphNode v'' _) -> otherNode `elem` v''

```

import System.Random
import Data.List

-- GraphNode is a list of vertices represented, and its edges
data GraphNode = GraphNode [Int] [Int] deriving (Show, Eq)
type Graph = [GraphNode]


pickEdge :: StdGen -> Graph -> Graph
pickEdge gen graph =
    let
        -- pick a random element in Graph
        (elem, gen') = randomR (1, length graph) gen
        (GraphNode v es) = graph !! elem
        --graph' = delete (GraphNode v es) graph
        graph' = graph

        -- pick a connected Node
        (nodeIndex, _) = randomR (1, length es) gen'
        otherNode :: Int
        otherNode = es !! nodeIndex
        myFilter :: GraphNode -> Bool
        myFilter = \(GraphNode v'' _) -> otherNode `elem` v'' 
        (GraphNode v' es') = head $ filter myFilter graph'
        graph'' = delete (GraphNode v' es') graph'
    in mergeNodes (GraphNode v es) (GraphNode v' es') : graph''

Upvotes: 2

Views: 189

Answers (1)

Sean Perry
Sean Perry

Reputation: 3886

You overwrote the function elem when you unpacked the result of randomR in the first line of your let statement. elem is now an Int.

Upvotes: 10

Related Questions