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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:11:49+00:00 2026-06-10T10:11:49+00:00

Say I have the following definitions data Book = Book {id :: Int, title

  • 0

Say I have the following definitions

data Book = Book {id :: Int, title :: String}
type Shelf = [Book]

Assuming I have a hypothetical function (upd is for update)

updShelf :: Shelf -> Shelf
updShelf all@(book : books) = updBook book : updShelf books

All fine so far. Now let’s say the updateBook function needs to refer to the updated book three books before it i.e updateBook for book at position 5 in bookshelf need to refer to book at position 2 (assume first three books need no such reference to update). No problem, I say, and modify my code as such:

updShelf :: Shelf -> Shelf
updShelf all@(book : books) prevBook = updBook book prevBook : updShelf books
                where prevBook = ???

What I need help is with is the prevBook function. Although I am not even sure if I am approaching this problem the right way. So, if you guys have any better suggestion to approach this problem differently, it would be highly appreciated

EDIT:

Thomas M. DuBuisson: Your solution won’t work for me. Here’s why:
Assume initial shelf (all) state as

Book {id=1, title="a"}
Book {id=2, title="b"}
Book {id=3, title="c"}
Book {id=4, title="d"}
Book {id=5, title="e"}
Book {id=6, title="f"}
Book {id=7, title="g"}
Book {id=8, title="h"}

then (drop 3 partialUpdate) is (using only ids rather than entire book statement):

updBook 4
updBook 5
updBook 6
updBook 7
updBook 8

zipWith’ ($) (drop 3 partialUpdate) (all) is :

updBook 4 1
updBook 5 2
updBook 6 3
updBook 7 4 -> YIKES! Older version of book 4!
updBook 8 5 -> YIKES! Older version of book 5!

In my case, I need books 7 and 8 to be updated against already updated versions of book 4 and 5,not the un-updated ones. I hope you understand what I mean to convey.

  • 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-10T10:11:51+00:00Added an answer on June 10, 2026 at 10:11 am

    This trick is related to tying the knot: we’ll use the answer while computing the answer. For the purposes of illustration, I’ll use type Book = Int instead.

    updateShelf :: Shelf -> Shelf
    updateShelf shelf = answer where
       answer  = zipWith updateBook shifted shelf
       shifted = replicate 3 Nothing ++ map Just answer
    
    -- some stupid implementation just for illustration
    updateBook :: Maybe Book -> Book -> Book
    updateBook Nothing          current = current + 1
    updateBook (Just threeBack) current = current + threeBack + 1
    

    Now, in ghci, we can verify that updateShelf is really using the updated versions:

    *Main> updateShelf [1,10,100,1000,10000]
    [2,11,101,1003,10012]
    

    As you can see, the first three are 1+1, 10+1, and 100+1, and the remaining two are 1000+(1+1)+1 and 10000+(10+1)+1, and are therefore using the updated previous values, just as you’d hope.

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

Sidebar

Related Questions

Say I have the following data IEnumerable<IEnumerable<int>> items = new IEnumerable<int>[] { new int[]
Say I have the following: string fromTime = 8:00am; string someDate = 06/01/2012; I
Lets say we have the following two class definitions. #include <iostream> #include <array> class
Say I have the following class: template<class T> struct A { static int value;
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
say I have the following <h3>This is my heading</h3> <p>This is my headingAnd this
Say I have the following chef role: name test description role for test run_list

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.