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

  • Home
  • SEARCH
  • 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 639431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:50:31+00:00 2026-05-13T20:50:31+00:00

I’m trying to re-learn systems analysis. I’ve got a lot of object-oriented thinking for

  • 0

I’m trying to re-learn systems analysis. I’ve got a lot of object-oriented thinking for which I’m not able to find equivalents in Haskell, yet.

A fictional system consists of Ambulance Stations, Ambulances and Crew. (It’s getting object-y already.) All this state can be wrapped up in a big SystemState type. SystemState [Stations] [Ambulances] [Crew]. I can then create functions which take a SystemState, and return a new SystemState.

module AmbSys
    ( version
    , SystemState
    , Station
    , Ambulance
    , Crew
    ) where

version = "0.0.1"

data SystemState = SystemState [Station] [Ambulance] [Crew] deriving (Show)

data Station = Station { stName :: String
                       , stAmbulances :: [Ambulance]
                       } deriving (Show)

data Ambulance = Ambulance { amCallSign :: String
                           , amStation :: Station
                           , amCrew :: [Crew]
                           } deriving (Show)

data Crew = Crew { crName :: String
                 , crAmbulance :: Ambulance
                 , crOnDuty :: Bool
                 } deriving (Show)

Here’s a ghci session where I create some data.

*AmbSys> :load AmbSys                 
[1 of 1] Compiling AmbSys           ( AmbSys.hs, interpreted )
Ok, modules loaded: AmbSys.
*AmbSys> let s = Station "London" []                
*AmbSys> let a = Ambulance "ABC" s []               
*AmbSys> let s' = Station "London" [a]
*AmbSys> let c = Crew "John Smith" a False        
*AmbSys> let a' = Ambulance "ABC" s [c]   
*AmbSys> let s'' = Station "London" [a']             
*AmbSys> let system_state = SystemState [s''] [a'] [c]
*AmbSys> system_state                                 
SystemState [Station {stName = "London", stAmbulances = [Ambulance {amCallSign = "ABC",
 amStation = Station {stName = "London", stAmbulances = []}, amCrew = [Crew 
 {crName = "John Smith", crAmbulance = Ambulance {amCallSign = "ABC", 
 amStation = Station {stName = "London", stAmbulances = []}, amCrew = []}, 
 crOnDuty = False}]}]}] [Ambulance {amCallSign = "ABC", amStation = Station {
 stName = "London", stAmbulances = []}, amCrew = [Crew {crName = "John Smith",
 crAmbulance = Ambulance {amCallSign = "ABC", amStation = Station {stName = "London",
 stAmbulances = []}, amCrew = []}, crOnDuty = False}]}] [Crew {crName = "John Smith",
 crAmbulance = Ambulance {amCallSign = "ABC", amStation = Station {stName = "London",
 stAmbulances = []}, amCrew = []}, crOnDuty = False}]

You can already see a couple of problems here:

  1. I have been unable to create a consistent SystemState – some of the values are ‘old’ values, such as s or s’, rather than s”.
  2. lots of references to the ‘same’ data have separate copies.

I could now create a function which takes a SystemState and a Crew member’s name which returns a new SystemState where that crew member is ‘off-duty’.

My problem is that I have to find and change the crew member in the Ambulance and the (identical copy of the) crew member in the SystemState.

This is possible for small systems, but real systems have many more linkages. It looks like a n-squared problem.

I am very aware that I am thinking about the system in an object-oriented way.

How would such a system be correctly created in Haskell?

Edit: Thanks to everyone for your answers, and those on reddit too http://www.reddit.com/r/haskell/comments/b87sc/how_do_you_manage_an_object_graph_in_haskell/

My understanding now seems to be that I can do the things I want in Haskell. On the downside, it seems to be that object/record/struct graphs aren’t ‘first class’ objects in Haskell (as they are in C/Java/etc.), because of the necessary lack of references. There’s just a trade off – some tasks are syntactically simpler in Haskell, some are simpler (and more unsafe) in C.

  • 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-13T20:50:31+00:00Added an answer on May 13, 2026 at 8:50 pm

    Small tip: If you use a recursive let or where (in a .hs file, I don’t think it works in ghci) you can at least set up the initial graph more easily as follows:

    ambSys = SystemState [s] [a] [c] where
        s = Station "London" [a]
        a = Ambulance "ABC" s [c]
        c = Crew "John Smith" a False
    

    This will get you to the state I think you’re trying to reach, but don’t try to use the derived Show instances 🙂 Updating states like these is another can of beans; I’ll give that some thought and see what I come up with.

    EDIT: I’ve thought about it some more, and here’s what I’d probably do:

    I would break the cycles in the object graph by using keys. Something like this would work (I used a similar approach when building real graphs):

    import qualified Data.Map as M
    
    version = "0.0.1"
    
    newtype StationKey = StationKey String deriving (Eq,Ord,Show)
    newtype AmbulanceKey = AmbulanceKey String deriving (Eq,Ord,Show)
    newtype CrewKey = CrewKey String deriving (Eq,Ord,Show)
    
    data SystemState = SystemState (M.Map StationKey Station) (M.Map AmbulanceKey Ambulance) (M.Map CrewKey Crew) deriving (Show)
    
    data Station = Station { stName :: StationKey
                           , stAmbulances :: [AmbulanceKey]
                           } deriving (Show)
    
    data Ambulance = Ambulance { amCallSign :: AmbulanceKey
                               , amStation :: StationKey
                               , amCrew :: [CrewKey]
                               } deriving (Show)
    
    data Crew = Crew { crName :: CrewKey
                     , crAmbulance :: AmbulanceKey
                     , crOnDuty :: Bool
                     } deriving (Show)
    
    ambSys = SystemState (M.fromList [(londonKey, london)]) (M.fromList [(abcKey, abc)]) (M.fromList [(johnSmithKey, johnSmith)]) where
        londonKey = StationKey "London"
        london = Station londonKey [abcKey]
        abcKey = AmbulanceKey "ABC"
        abc = Ambulance abcKey londonKey [johnSmithKey]
        johnSmithKey = CrewKey "John Smith"
        johnSmith = Crew johnSmithKey abcKey False
    

    And then you can start defining your own state-modifying combinators. As you can see, the building of states is more verbose now, but showing works nicely again!

    Also I would probably set up a typeclass to make the link between Station and StationKey etcetera types more explicit, if that becomes too cumbersome. I didn’t do that in my graph code, as there I had only two key types, which also were distinct, so the newtypes weren’t necessary.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
i got an object with contents of html markup in it, for example: string
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.