Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6972949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:02:31+00:00 2026-05-27T17:02:31+00:00

I’m using a Data.Graph Graph to model a simulation in Haskell. The simulation is

  • 0

I’m using a Data.Graph Graph to model a simulation in Haskell. The simulation is limited to a 2D grid which my graph models. A node at each point on the grid below will contain a Maybe Molecule type so there could be a molecule present or just Nothing.

1  - 2  - 3  
|    |    |  
4  - 5  - 6  
|    |    |  
7  - 8 -  9  

I have set up this representation but when it comes to updating the position of a molecule I feel I’m going the long way around the issue. What I’ve done so far is stripped all the nodes into a list of nodes. I’ve written a function to swap the two items in this list of nodes. But now when I come to zip everything back together I come into problems because to generate a new graph I need a list of vertices which I obtain easily from the vertices Graph function. But I also need to zip that with the list of vertices the edge touches. Unfortunately Data.Graph’s edges Graph function returns a list of tuples of type Edge which isn’t immediately helpful for generating a graph as far as I can see, although I could write a function to derive the list vertices which have edges to a vertex. Doing so seems to be enough work for me to wonder am I missing the point is there a Graph function out there which does just take a graph and return a graph with an updated node?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:02:32+00:00Added an answer on May 27, 2026 at 5:02 pm

    FGL has this great “context” mechanism that lets you pattern match on a graph query. You can imagine this as tugging on a chosen vertex so that it sits to the side of the rest of the graph. This lets you look at how that that vertex is connected to the rest of the graph.

    {-# LANGUAGE TupleSections #-}
    import Control.Applicative
    import Control.Arrow
    import Data.Graph.Inductive
    
    -- Example graph from SO question.
    graph :: Gr (Maybe Int) ()
    graph = mkGraph (map (id&&&Just) [1,2,3,4,5,6,7,8,9])
                    (map (\(x,y) -> (x,y,())) $
                         concatMap gridNeighbors [1..9])
      where gridNeighbors n = map (n,) 
                            . filter ((&&) <$> valid <*> not . boundary n) 
                            $ [n-3,n-1,n+1,n+3]
            valid x = x > 0 && x < 10
            boundary n x = case n `rem` 3 of
                             0 -> x == n + 1
                             1 -> x == n - 1
                             _ -> False
    
    -- Swap the labels of nodes 4 and 7
    swapTest g = case match 4 g of
                   (Just c4, g') -> case match 7 g' of
                                      (Just c7, g'') -> setLabel c4 (lab' c7) & 
                                                        (setLabel c7 (lab' c4) &
                                                         g'')
                                      _ -> error "No node 7!"
                   _ -> error "No node 4!"
      where setLabel :: Context a b -> a -> Context a b
            setLabel (inEdges, n, _, outEdges) l = (inEdges, n, l, outEdges)
    

    You can try running swapTest graph to see that the labels for nodes 4 and 7 in your diagram are swapped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.