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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:24:43+00:00 2026-05-28T01:24:43+00:00

How can I construct the function segs that returns a list of all contiguous

  • 0

How can I construct the function segs that returns a list of all contiguous segments in a list?
For example, (segs '(l i s t)) should produce the following answer:

(() (t) (s) (s t) (i) (i s) (i s t) (l) (l i) (l i s) (l i s t))

I’m especially interested in how to solve this problem in accordance with the design principles described in HtDP (no, this is not the problem from the book, so please feel free to discuss it!) How to solve it? Which principles to use in program derivation?

  • 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-28T01:24:44+00:00Added an answer on May 28, 2026 at 1:24 am

    Start by building up a set of related examples, most trivial first:

    (equal? (segs '())
            (list '()))
    (equal? (segs '(z))
            (list '()
                  '(z)))
    (equal? (segs '(y z))
            (list '() '(z)
                  '(y) '(y z)))
    (equal? (segs '(x y z))
            (list '() '(z) '(y) '(y z)
                  '(x) '(x y) '(x y z)))
    

    By looking at the examples you can make an observation (which I’ve used the formatting to highlight): the answer for each example includes the all of the elements from the answer for the previous example. In fact, the contiguous subsequences of a non-empty list are just the contiguous subsequences of its tail together with the non-empty prefixes of the list itself.

    So put the main function on hold and write non-empty-prefixes

    non-empty-prefixes : list -> (listof non-empty-list)
    

    With that helper function, it’s easy to write the main function.

    (Optional) The resulting naive function has bad complexity, because it repeats calls to non-empty-prefixes. Consider (segs (cons head tail)). It calls (non-empty-prefixes tail) twice: once because it calls (segs tail) which calls (non-empty-prefixes tail), and again because it calls (non-empty-prefixes (cons head tail)) which calls (non-empty-prefixes tail) recursively. That means the naive function has unnecessarily bad complexity.

    The problem is that (segs tail) computes (non-empty-prefixes tail) and then forgets it, so (segs (cons head tail)) has to redo the work. The solution is to hold on to that extra information by fusing segs and non-empty-prefixes into a single function that computes both answers:

    segs+ne-prefixes : list -> (values (listof list) (listof non-empty-list))
    

    Then define segs as an adapter function that just drops the second part. That fixes the main issue with the complexity.

    (Edited to add) Regarding segs+ne-prefixes: here’s one way to define non-empty-prefixes. (Note: the empty list has no non-empty prefixes. No need to raise an error.)

    ;; non-empty-prefixes : list -> (listof non-empty-list)
    (define (non-empty-prefixes lst)
      (cond [(empty? lst)
             empty]
            [(cons? lst)
             (map (lambda (p) (cons (first lst) p))
                  (cons '() (non-empty-prefixes (rest lst))))]))
    

    And segs looks like this:

    ;; segs : list -> (listof list)
    (define (segs lst)
      (cond [(empty? lst) (list '())]
            [(cons? lst)
             (append (segs (rest lst))
                     (non-empty-prefixes lst))]))
    

    You can fuse them like this:

    ;; segs+ne-prefixes : list -> (values (listof list) (listof non-empty-list))
    ;; Return both the contiguous subsequences and the non-empty prefixes of lst
    (define (segs+ne-prefixes lst)
       (cond [(empty? lst)
              ;; Just give the base cases of each function, together
              (values (list '())
                      empty)]
             [(cons? lst)
              (let-values ([(segs-of-rest ne-prefixes-of-rest)
                            ;; Do the recursion on combined function once!
                            (segs+ne-prefixes (rest lst))])
                (let ([ne-prefixes
                       ;; Here's the body of the non-empty-prefixes function
                       ;; (the cons? case)
                       (map (lambda (p) (cons (first lst) p))
                            (cons '() ne-prefixes-of-rest))])
                  (values (append segs-of-rest ne-prefixes)
                          ne-prefixes)))]))
    

    This function still follows the design recipe (or it would, if I had shown my tests): in particular, it uses the template for structural recursion on a list. HtDP doesn’t talk about values and let-values, but you could do the same thing with an auxiliary structure to group the information.

    HtDP talks about complexity a little bit, but this sort of regrouping of computation is usually discussed more in an algorithms course, under “dynamic programming and memoization”. Note that an alternative to fusing the two functions would have been to memoize non-empty-prefixes; that also would have fixed the complexity.

    One last thing: the arguments to append near the end should be reversed, to (append ne-prefixes segs-of-rest). (Of course, that means rewriting all of the tests to use the new order, or writing/finding an order-insensitive list comparison function.) Try benchmarking the two versions of the function on a large list (around 300-400 elements), see if you can tell a difference, and see if you can explain it. (This is more algorithms material, not design.)

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

Sidebar

Related Questions

I can construct a list of values of a function evaluated at a set
In python, I can construct my optparse instance such that it will automatically filter
How can I construct a MSBuild ItemGroup to exclude .svn directories and all files
How can I construct the following string in an Excel formula: Maurice "The Rocket"
Given that I have a pointer to a function (provided by dlsym() for example)
How can I construct a boost::python::object from a std::function ?
I'm trying to create a custom function that I can place in a queryset
Doctrine documentation says you can use public function construct() { ... } as a
In the following construct: (function(){ var x = function(){ alert('hi!'); } var y =
I'm attempting to construct a function that will perform a sanity check on the

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.