Kochev
Kochev

Reputation: 35

Connect vertices of the graph in Haskell

Graph stored as a list of tuples. The first element of tuple - vertex, the second element tuple - vertices with which it is connected.

headName-vertex which I want to connect[:t=Srting]**

elements-vertises which I want to connect[:t=List]**

A can connect vertices so [(1,[2]),(2,[])], but i want connect so [(1,[2]),(2,[1])]. Kind of did, but does not work. What is wrong? Help, please.

connect_command graph headName []=(graph,"You didnt enter elements to connect!\n")
connect_command graph headName elements=
    if (has_element graph headName) then ((update_connect graph graph headName elements []),"Element "++headName++"  connected with "++listToString (checked_elements graph headName elements []) []++"\n")
    else (graph,"Element with "++headName++" name not found!")


update_connect _ [] _ _ result=result 
update_connect mainGraph (item:graph) headName (i:elements) result=
    if ((fst item)==headName) then (update_connect mainGraph graph headName elements (result++[((fst item),(checked_elements mainGraph headName elements []))]))
    else if ((fst item)==i) then (update_connect mainGraph graph headName (i:elements) (result++[((fst item),[headName])]))
    else (update_connect mainGraph graph headName elements (result++[item]))


checked_elements _ _ [] result=result
checked_elements graph headName (item:elements) result=
    if (has_element graph item)&& (item /=headName)then checked_elements graph headName elements (result++[item])
    else (checked_elements graph headName elements result)

headName-vertex which I want to connect*[:t=Srting]*

elements-vertises which I want to connect*[:t=List]*

Upvotes: 0

Views: 285

Answers (1)

MathematicalOrchid
MathematicalOrchid

Reputation: 62808

  1. "does not work" is kind of vague.

  2. You appear to be trying to use lists of key/value tuples as a dictionary. The standard libraries already provide a real dictionary, so you don't have to manually implement functions like has_element by yourself. Look at Data.Map.

  3. Actually, the standard libraries already have Data.Graph, which may already do what you need. (Depending on whether you actually need a graph, or whether you're writing this as a way to learn Haskell.)

  4. At any rate, defining some custom data types rather than having everything as lists and tuples and strings would probably help you greatly in narrowing down what's going wrong. (It could certainly help us try to figure out what's supposed to be happening here...)

Upvotes: 1

Related Questions