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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:55:07+00:00 2026-06-17T22:55:07+00:00

I am trying to define a function, that would take a list and split

  • 0

I am trying to define a function, that would take a list and split it down n without using take, drop or grouped

  def mySplit[X](n: Int, xs: List[X]): (List[X], List[X]) = {
    if (n <= 0) (Nil, xs)
    else
    if (n >= xs.size) (xs, Nil)
    else
    if (n < xs.tail.size) (xs.head :: mySplit(n, xs.tail), Nil) 
                     else (Nil, xs.head :: mySplit(n, xs.tail))
  }

Here is what I am thinking. If n < xs.tail.size we can build up tuple 1 (representing part 1), else we can work on tuple 2 (second part of the list).

The above does not work. It seems that it does not like the :: when I am constructing part of a tuple.

Am I approaching this the right way?

Example: mySplit(3, (1 to 5).toList) should return (List(1,2,3), List(4,5))

  • 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-17T22:55:08+00:00Added an answer on June 17, 2026 at 10:55 pm

    Here is my take, rewritten from scratch:

    def mySplit[X](n: Int, xs: List[X]): (List[X], List[X]) = {
      xs match {
        case Nil => (Nil, Nil)
        case head :: tail =>
          if(n == 0)
            (Nil, xs)
          else
            mySplit(n - 1, tail) match {
              case (before, after) =>
                (head :: before, after)
            }
      }
    }
    

    Or alternatively and shorter:

    def mySplit[X](n: Int, xs: List[X]): (List[X], List[X]) = {
      n match {
        case 0 => (Nil, xs)
        case s =>
          mySplit(s - 1, xs.tail) match {
            case (before, after) =>
              (xs.head :: before, after)
          }  
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to define a function that would to that: var photoWidth =
I am trying to define a function which would take a Double -> Double
when trying to define a function that would remove the largest subset of set
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I'm trying to define a function using template template parameters (I just want to
I'm trying to define a very simple function in Drools as below: import java.util.List;
I am trying to write a function that will take an xml object, an
I am trying to define a function that will include a variable n where
I'm struggling with PL/SQL functions. I'm trying to write a function which would take
I'm trying to define a function in Haskell that takes an integer argument c

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.