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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:53:02+00:00 2026-05-27T22:53:02+00:00

Example: How to convert list: ‘(0 1 2 3 4 5 6 7 8

  • 0

Example:
How to convert list:
‘(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)

Into list of lists:
‘((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15))

Based on answers provided here so far, this is what I’ve come up with:

First define function to take up to ‘n’ elements from beginning of the list:

(define (take-up-to n xs)
  (define (iter xs n taken)
    (cond
      [(or (zero? n) (empty? xs)) (reverse taken)]
      [else (iter (cdr xs) (- n 1) (cons (car xs) taken))]))
  (iter xs n '()))

Second is similar function for the rest of list:

(define (drop-up-to n xs)
  (define (iter xs n taken)
    (cond
      [(or (zero? n) (empty? xs)) xs]
      [else (iter (cdr xs) (- n 1) (cons (car xs) taken))]))
  (iter xs n '()))

This could have been done as one function that returns two values and Racket has a function ‘split-at’ that produces same result, but I did this as an exercise.

ps. Is this correct use of tail recursion ?

Than split-into-chunks can be written like this:

(define (split-into-chunks n xs)
  (if (null? xs)
      '()
      (let ((first-chunk (take-up-to n xs))
            (rest-of-list (drop-up-to n xs)))
        (cons first-chunk (split-into-chunks n rest-of-list)))))

pps. Can this one be improved even more or is it ‘good enough’ ?

  • 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-27T22:53:02+00:00Added an answer on May 27, 2026 at 10:53 pm

    There’s a common utility function in Scheme, in the SRFI-1 library (which Racket offers, but I don’t recall how to import it), called take, which takes the initial n elements from a list:

    (take 4 '(0 1 2 3 4 5 6 7 8))
    => '(0 1 2 3)
    

    There is also in the same library a function called drop which removes the initial n elements from a list:

    (drop 4 '(0 1 2 3 4 5 6 7 8))
    => '(4 5 6 7 8)
    

    You can break down the problem into smaller pieces by using functions like these. So, a first (but incorrect) approximation to solving your problem would be this:

    (define (split-into-chunks n xs)
      (if (null? xs)
          '()
          (let ((first-chunk (take n xs))
                (rest (drop n xs)))
            (cons first-chunk (split-into-chunks n rest)))))
    

    As I noted, however, this solution is suboptimal. Why? Because (take n xs) gives you an error when the list xs has fewer than n elements; translated to your problem, if the list has a non-n multiple of elements you get an error. However, you can fix this by writing a pair of functions, take-up-to and drop-up-to that work like take and drop but don’t have that problem. So example usage of the functions would look like this:

    (take-up-to 4 '(0 1 2))
    => '(0 1 2)
    
    (drop-up-to 4 '(0 1 2))
    => '()
    

    This is as much as I’m going to tell you. I suggest you do these things:

    • Write your own implementations of take, drop, take-up-to and drop-up-to, and use them to write the function you’re trying to implement.
    • Skim through the documentation for the SRFI-1 library and familiarize yourself with what the functions there do. A lot of these list problems break down into easy combinations of these functions, so you want to know about them.
    • Learn how to import this library into Racket. (Can’t help you there.)
    • As an exercise, try your hand at writing your own implementations of some of the SRFI-1 functions. (It’s OK to simplify them a bit for the sake of the exercise; for example, while many of these functions will deal with multiple list arguments, it’s OK for exercise purposes to write a version that deals with just one list.)

    EDIT: Here’s simple implementation of take-up-to:

    (define (take-up-to n xs)
      (if (or (zero? n) (null? xs))
          '()
          (cons (car xs) (take-up-to (- n 1) (cdr xs)))))
    

    It’s possible to improve on this still some more to use only tail calls (and thus run in constant space). That’s left as yet another exercise.

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

Sidebar

Related Questions

How to convert last 3 digits of number into 0 example 3444678 to 3444000
Here's an example of the query I'm trying to convert to LINQ: SELECT *
How do I convert strings like this is an example to this-is-an-example under ruby?
Is there some way to convert between F# List and F# Tuple? For example:
Is there a way to convert a list of values into a comma-delimited string
To illustrate what I mean by this, here is an example messages = [
In the below example, how can I easily convert eventScores to List<int> so that
I am trying to convert a set object to list...for example p=list('abc') is not
I have a generic list of objects, for example: List<Photo> and want to convert
Given a Circularly doubly linked list... How can i convert it into Binary Search

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.