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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:09:49+00:00 2026-06-16T13:09:49+00:00

I’d like to be able to use a single variable multiple times within one

  • 0

I’d like to be able to use a single variable multiple times within one pattern, so that it will only match if the same value is present in all places, such as

list match {
  case x :: x :: xs => // recurse
}

which would match List(1,1,2) but would not match List(1,2,1). But this does not compile with error: x is already defined as value x.

In researching this question, I found out that I can also include a guard in the case clause, so I can do

list match {
  case x1 :: x2 :: xs if x1==x2 => // recurse
}

which seems to work the same way (it does, right?). This is good, but it wouldn’t look as clean if I wanted the same value in many places, like

list match {
  case x1::x2::x3::x4::xs if x1==x2 && x2==x3 && x3==x4 => // recurse
}

Is there any more elegant way I can do this?


A few notes: Yes, I am just learning scala, if that wasn’t clear, so I’m not sure this is something I’d ever really want to do, but I’m just interested in what’s possible. In that regard, I’m not really looking for a completely different solution, like takeWhile or filter or something, but more so am specifically interested in pattern matching.

  • 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-16T13:09:51+00:00Added an answer on June 16, 2026 at 1:09 pm

    Scala doesn’t provide quite that much flexibility with its matches (which may be a good thing, as one has to be aware of errors arising from unintentional variable re-use).

    If you have a large number of identical items, you might want to consider a nested match (but note that you won’t fail out of the inner match to be completed later down the outer match, so you have to handle everything locally):

    list match {
      case x :: rest => rest match {
        case `x` :: `x` :: `x` :: xs => println("Four of the same")
        case _ => println("Well, nonempty at least")
      }
      case _ => println("Boring, there's nothing here!")
    }
    

    Note the backticks which mean “we’ve already got this variable, check against it, don’t set it!”.

    Alternatively, if you have specialized functionality that you use repeatedly, you can create a custom matcher:

    object FourOf {
      def unapplySeq(xs: List[Int]): Option[(Int, List[Int])] = xs match {
        case x :: y :: z :: a :: rest if x==y && y==z && z==a => Some((x,rest))
        case _ => None
      }
    }
    

    and then use it whenever you need that complicated pattern:

    list match {
      case FourOf(x,rest) => println("four of the same")
      case x :: more => println("Nonempty")
      case _ => println("Yawn")
    }
    

    Neither of these are quite as tidy and flexible as what you were apparently hoping for, but then again, I’m not sure flipping between assigning and testing the same variable in a match statement is a good way to write clear code anyway.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.