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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:11:09+00:00 2026-05-27T05:11:09+00:00

Learn You a Haskell mentions difference lists (search for this term on that page),

  • 0

Learn You a Haskell mentions difference lists (search for this term on that page), where a list l is represented not directly but as a function (l++). This allows more efficient concatenation on both left and right. Concatenation becomes function composition, and one can finally convert to a real list by ($[]). I was wondering what operations one can support efficiently on difference lists. For example, the equivalent of (:) for difference lists is

\x l -> (x:) . l

Can one efficiently implement head and tail for difference lists? Here is the obvious implementation:

headTailDifList :: ([a] -> [a]) -> (a, [a] -> [a])
headTailDifList dl = (head l, ((tail l)++))
    where
    l = dl []

For real lists, \l -> (head l, tail l) runs in constant time. What about this headTailDifList? Perhaps due to lazy evaluation only the first element of l will be evaluated?

  1. What does it even mean to ask if this runs in constant time, given that a difference list is a function and not an actual “value”?
  2. Does headTailDifList run in constant time?
  3. Is there some other constant-time implementation? Here’s a candidate:

    headTailDifList dl = (head (dl []), tail.dl )
    

    However, the tail part does not throw an exception if dl is id (the empty difference list).

  • 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-27T05:11:09+00:00Added an answer on May 27, 2026 at 5:11 am

    Edit: added more about the thunk.

    Begin by looking just at the conversion from a difference list to a regular list. This operation alone takes only constant time, because no evaluation is required. The resulting list will exist as a thunk which will be structured something like this:

    enter image description here

    The base definition of (++) is right-associative and needs to step through the entire first argument before it can continue with the second argument. This matches perfectly with the nested structure produced by the difference list, as each (++) gets a single list chunk as its first argument. Furthermore, because (++) is a good list producer, the entire structure exists lazily. Although fully evaluating it takes O(n), evaluating just the head is O(k) where k=number of chunks.

    Consider if a,b == []; c = [1..]. Then head would need to first check that a and b are empty before moving on to c and finding the first element. In the worst case head would traverse the entire structure before finding the empty list constructor. Practically this is a very rare case however, and even then it’s not particularly harmful because encountering an empty chunk and moving on is a constant-time operation.

    In general use, evaluating a difference list should differ from a regular list in time complexity by only a constant factor for equivalent operations.

    Producing just the first element of a difference list doesn’t require O(n) time, as can easily be demonstrated by using infinite lists:

    *Dl Control.Monad Control.Applicative> head $ ($ []) $ toDl [1..] . toDl [4..]
    1
    (0.01 secs, 2110104 bytes)
    
    *Dl Control.Monad Control.Applicative> fmap (head . ($ [])) $ headTailDifList (toDl [1..])
    (1,2)
    (0.00 secs, 2111064 bytes)
    
    *Dl Control.Monad Control.Applicative> fmap (head . ($ [])) $ headTailDifList (toDl [1..] . toDl [3..] . toDl [] . toDl [5..])
    (1,2)
    (0.01 secs, 3105792 bytes)
    
    -- create a difference list
    toDl :: [a] -> ([a] -> [a])
    toDl l = (l ++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

LISP or Haskell, I need to learn functional programming, but I heard that lisp
I'm trying to compile this function from Learn You a Haskell for Great Good
This code is used in Learn You A Haskell to explain how (++) can
I've been doing my annual attempt to learn Haskell this weekend, and as ever
In Learn You a Haskell for Great Good! author claims that Applicative IO instance
Seems that there are not much work done on Haskell for web. People uses
So I'm making a list of prime numbers to help me learn haskell using
I'm going through the 'Learn you a Haskell' tutorial and I am at this
Started to learn Haskell, I decided to get acquainted with Parsec, but there were
This code (taken from Learn You A Haskell ): main = do putStr Hey,

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.