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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:12:49+00:00 2026-06-03T01:12:49+00:00

I am trying to solve the following problem from Scala for the impatient .

  • 0

I am trying to solve the following problem from Scala for the impatient. The question is as follows:

Using pattern matching, write a function swap that swaps the first two elements of an array provided its length is at least two.

My solution is:

def swap(sArr:Array[Int]) = sArr.splitAt(2) match { 
                               case (Array(x,y),Array(z)) => Array(y,x,z)
                               case (Array(x,y),Array()) => Array(y,x)
                               case _ => sArr
                            }

My problem is with the first case statement. I think it would pattern-match something like (Array(1,2),Array(3)) whereas I intend it to pattern-match (Array(1,2),Array(3,4,5.....))

Can somebody point out how that would be possible.

Thanks

  • 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-03T01:12:51+00:00Added an answer on June 3, 2026 at 1:12 am

    The problem with your code is that Array(z) means “match a one-element array”. What you want is for z to be the whole array, no matter how many elements:

    def swap(sArr: Array[Int]) = 
      sArr.splitAt(2) match { 
        case (Array(x, y), z) => Array(y, x) ++ z
        case _ => sArr
    }
    

    However, I would write it with the sequence-matching syntax _* so that you don’t have to manually split the array:

    def f(a: Array[Int]) = 
      a match {
        case Array(x, y, z @ _*) => Array(y, x) ++ z
        case _ => a
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve the following codechef problem using scala. The statement of
I am trying to solve Project Euler problem in Clojure using recursion. The following
I am trying to solve the following problem: Find the largest palindrome made from
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve the following problem with Puppet: I have multiple nodes.
I am trying to solve the following problem. Lets say you have the the
I am trying to solve the following question : A sequence in which the
I am trying to solve the following question which was part of a programming
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
Before starting writing this question, i was trying to solve following // 1. navigate

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.