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 7979281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:44:08+00:00 2026-06-04T09:44:08+00:00

I have a structure that is instantiated in a parent function and I want

  • 0

I have a structure that is instantiated in a parent function and I want to modify that instantiated data with calls to functions from that parent function. Here’s a contrived example:

import Data.List

data MyLists = MyLists {
    myInts :: [Int],
    myBools :: [Bool]
} deriving (Show)

addIntToList :: Int -> MyLists -> MyLists
addIntToList x main_lists =
    main_lists { myInts = Data.List.insert x my_ints }
    -- might make a call to another child function that modifies main_list here, and so on (i.e., this is the vertical problem I see with this structuring)
        where
            my_ints = myInts main_lists

main :: IO()
main = do
    let my_main_lists = MyLists [1,2,3] [False, True, False]
    let my_new_main_lists = addIntToList 4 my_main_lists
    print my_new_main_lists
    let my_new_new_main_lists = addBoolToList True my_new_main_lists
    print my_new_new_main_lists
    -- and so on (this is the lateral problem I see with this code structuring)

What are the alternatives ways to structure this code or accomplish tasks similar to this? Is there a more concise way?

I should add that this gets particularly smelly (ie. code smell) once you’re making a long chain of function calls to child functions; they all end up needing to return a new MyLists or just returning main_list without having done anything to it. That, and the parents might also have to deal with MyList and another return value (e.g., -> (Bool, MyList)).

So, you can imagine a tree structure of function calls all requiring a MyList parameter and return value; that doesn’t seem optimal.

Here’s a more concrete example of the kind of thing I’m talking about. Browse the code at https://github.com/mokehehe/monao (a super mario clone in haskell). You’ll see that state.monad is never used, and that there are upper level structures that must flow throughout the code (e.g., GameGame in Main.hs).

  • 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-06-04T09:44:11+00:00Added an answer on June 4, 2026 at 9:44 am

    You can make it a little more concise by using the RecordWildCards extension:

    {-# LANGUAGE RecordWildCards #-}
    import Data.List (insert)
    
    addIntToList :: Int -> MyLists -> MyLists
    addIntToList x ml@MyLists{..} = ml{ myInts = insert x myInts }
    

    The MyLists{..} pattern dumps the properties of the MyLists record into scope. Thus, we can easily reference the old myInts when initializing the new myInts.

    Conversely, if you use .. in expression context, it will fill in uninitialized properties with corresponding names in scope. The addIntToList function can also be written as:

    addIntToList x MyLists{..} = MyLists{ myInts = insert x myInts, .. }
    

    One more cool thing you can do with record wildcards:

    someAction = do
        myInts  <- getInts :: IO [Int]
        myBools <- getBools :: IO [Bool]
        return MyLists{..}
    

    Also, Data.List.insert is a bit verbose. You can just say insert, since this import:

    import Data.List
    

    will import all of the names from Data.List into the namespace. If you don’t like this (e.g. because you want to define a function in your own module called insert), you can import it qualified:

    import qualified Data.List as List
    
    … List.insert …
    

    As far as using MyLists in a program, the StateT monad transformer is quite helpful:

    {-# LANGUAGE RecordWildCards #-}
    import Control.Monad.State
    
    ...
    
    printList :: StateT MyLists IO ()
    printList = liftIO . print =<< get
    
    program :: StateT MyLists IO ()
    program = do
        printList
        modify $ addIntToList 4
        printList
        modify $ addBoolToList True
        printList
    
    main :: IO()
    main = evalStateT program $ MyLists [1,2,3] [False, True, False]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple data structure that I have defined in Lisp: ;;Data
I have a structure that roughly looks like this: List<ProductLine> -> ID Name ...
I have a directory structure that looks like this: /a/f.xml /b/f.xml /c/f.xml /d/f.xml What
I have this menu structure that is used to navigate through content slider panels.
I have a tree structure that can be n-levels deep, without restriction. That means
I have a menu structure that is supposed to highlight each item as it
I have an XML structure that is 4 deep: <?xml version=1.0 encoding=utf-8?> <EmailRuleList xmlns:xsd=EmailRules.xsd>
I have a queue structure that is being used by several pthreads. The threads
I have an array structure that looks like this: Array ( [0] => Array
I currently have a table structure that looks something like this(some details omitted): ColumnName

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.