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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:25:07+00:00 2026-05-27T12:25:07+00:00

I have the following list (it’s a length 2 list, but in my assignment

  • 0

I have the following list (it’s a length 2 list, but in my assignment I have a length +n list)

xxs = [(11,22,[(33,33,33),(44,44,44)]),(55,66,[(77,77,77),(88,88,88)])]

I’m trying to “replace” one 3-tuple (p1 or p2 or p3 or p4 from the image bellow) by list index (n) and by sub-list index (p).

Visual of list breakdown

The function, at the end, should be like:

fooo newtuple n p = (…)

For example: (replace p3 for (98,98,98):

fooo (98,98,98) 2 1 
[(11, 22, [(33,33,33) , (44,44,44)]) , (55, 66, [(98,98,98),(88,88,88)])]  

I planned the code like following this steps:

  1. Access the pn that I want to change. I manage to achieve it by:

    fob n p = ((aux2 xxs)!!n)!!p
       where aux2 [] = []
             aux2 ((_,_,c):xs) = c:aux2 xs
    
  2. “replace” the 3-tuple. I really need some help here. I’m stuck. the best code (in my head it makes some sense) that I’ve done: (remember: please don’t be too bad on my code, I’ve only been studying Haskell only for 5 weeks)

    foo n p newtuple = fooAux newtuple fob 
         where fooAux _ [] = [] 
               fooAux m ((_):ds) = m:ds
               fob n p = ((aux2 xxs)!!n)!!p
                  where aux2 [] = []
                        aux2 ((_,_,c):xs) = c:aux2 xs
    
  3. Finally I will put all back together, using splitAt.

Is my approach to the problem correct? I really would appreciate some help on step 2.

  • 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-27T12:25:08+00:00Added an answer on May 27, 2026 at 12:25 pm

    I’m a bit new to Haskell too, but lets see if we can’t come up with a decent way of doing this.

    So, fundamentally what we’re trying to do is modify something in a list. Using functional programming I’d like to keep it a bit general, so lets make a function update.

    update :: Int -> (a -> a) -> [a] -> [a]
    update n f xs = pre ++ (f val) : post
      where (pre, val:post) = splitAt n xs
    

    That will now take an index, a function and a list and replace the nth element in the list with the result of the function being applied to it.

    In our bigger problem, however, we need to update in a nested context. Luckily our update function takes a function as an argument, so we can call update within that one, too!

    type Triple a = (a,a,a)
    type Item = (Int, Int, [Triple Int])
    
    fooo :: Triple Int -> Int -> Int -> [Item] -> [Item]
    fooo new n p = update (n-1) upFn
       where upFn (x,y,ps) = (x,y, update (p-1) objFn ps)
             objFn _ = new
    

    All fooo has to do is call update twice (once within the other call) and do a little “housekeeping” work (putting the result in the tuple correctly). The (n-1) and (p-1) were because you seem to be indexing starting at 1, whereas Haskell starts at 0.

    Lets just see if that works with our test case:

    *Main> fooo (98,98,98) 2 1 [(11,22,[(33,33,33),(44,44,44)]),(55,66,[(77,77,77),(88,88,88)])]
    [(11,22,[(33,33,33),(44,44,44)]),(55,66,[(98,98,98),(88,88,88)])]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following list of time zones that a user can choose from,
I have the following code: List<int> intList = new ArrayList<int>(); for (int index =
I have the following list: [A;AA;ABC;BCD;B;C] I am randomly extracting an element from the
I have the following code: for(var i = 0; i < list.length; i++){ mc_cli.get(list[i],
I have a following code in coffeescript to recursively list directories (converted from javascript):
I have the following list of tuples: items = [ ('john jones', ['Director', 'Screenwriter',
I have the following List<int> collection and I need to find the highest integer
I have the following list <ul> <li id=item1>Item 1</li> <li id=item2>Item 2</li> <li>Item 3</li>
I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,
I have the following list: <s:List id=list x=4 y=4 width=99% height=99% dataProvider={recordingsShown} itemRenderer=components.VideoItemRenderer useVirtualLayout=false

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.