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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:00:34+00:00 2026-06-16T18:00:34+00:00

I’m currently working on an implementation of Project Euler problem 40 and am trying

  • 0

I’m currently working on an implementation of Project Euler problem 40 and am trying to figure out how to take multiple items from a list in Haskell without starting the list over.

Currently, I have a list champernowne of type Integral a => [a] which will return an infinite list of the digits of Champernowne’s constant, then I take the first, tenth, etc. terms off of this sequence and multiply them to get the answer. The actual code for this is:

ans = (product . map (champernowne !!)) [0, 9, 99, 999, 9999, 99999]

The problem with this implementation is (I’m assuming) that Haskell will go through the list from the beginning of the sequence each time that it wants to get a new term. How can I make it so that haskell only goes through the sequence from elements 1 to 1 000 000 and then pulls the terms out of the middle? I’ve already tried scanl in hope that the lazy evaluation would aid me, but it did not:

ans = (product . head . scanl (flip drop) champernowne) [10, 90, 900, 9000, 90000]

Just to clarify, the first bit of code does work, but I’m trying to improve my implementation to be a bit more efficient.

  • 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-16T18:00:35+00:00Added an answer on June 16, 2026 at 6:00 pm

    The efficient method to solve that problem is to compute the digits without constructing the list.

    But if your desired indices are given in ascending order, you can get that without starting from the fron for each by computing the relative offsets and dropping the appropriate number of items to reach the next desired index

    -- supposes the list of indices in ascending order
    indices :: [a] -> [Int] -> [a]
    indices xs is = go xs offsets
      where
        offsets = zipWith (-) is (0:is)
        go ys (k:ks) = case drop k ys of
                         z:zs -> z : go (z:zs) ks
                         _    -> []
        go _ [] = []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was

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.