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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:08:36+00:00 2026-05-15T06:08:36+00:00

Scala Newbie alert: basically I’m trying to do something like this: where I pattern

  • 0

Scala Newbie alert:

basically I’m trying to do something like this: where I pattern match and return a String.

scala> def processList(list: List[String], m: String): String={list foreach (x=> m match{
 | case "test" => "we got test"                                                      
 | case "test1"=> "we got test1"})}                                                  

:10: error: type mismatch;
found : Unit
required: String
def processList(list: List[String], m: String): String={list foreach (x=> m match{

I know I can set a var and return it after the for comp… but that doesn’t seem to be the Scala way.

  • 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-15T06:08:37+00:00Added an answer on May 15, 2026 at 6:08 am

    It is not completely clear to me what exactly it is that you are trying to do. Do you just want to test whether a certain element is present in the list? Or do you want to return a list of strings with some transformation? The latter, for example, can be written like so:

    
    scala> def processList(l: List[String]): List[String] = l map {s => s match {
           case "test" => "we got test"
           case "test1" => "we got test1"
           case _ => "we got something else"
         }
     }
    scala> processList(List("test", "test1", "test2", "test3"))
    res: List[String] = List(we got test, we got test1, we got something else, we got something else)
    

    And for the former you could write something like:

    
    scala> def exists(l: List[String], m: String): String = {
        if (l exists (s => s == m))
        m + " exists"
        else 
        m + " does not exist"
    }
    exists: (l: List[String],m: String)String
    
    scala> val l = List("test1", "test2", "test3")
    l: List[java.lang.String] = List(test1, test2, test3)
    
    scala> exists(l, "test1")
    res0: String = test1 exists
    
    scala> exists(l, "test2")
    res1: String = test2 exists
    
    scala> exists(l, "test8")
    res2: String = test8 does not exist
    

    In any case: the foreach method on a List loops over every element in a list, applying the given function to each element. It is mainly used for side-effecting, such as printing something to the console, or writing to a file. The function passed to the foreach method,must return the Unit type, which is like void in Java. You cannot return a single String from it, therefore.

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

Sidebar

Related Questions

Scala/Android newbie question. I'm trying to rewrite this simple code from Java to Scala.
I'm a Scala newbie I'm afraid: I'm trying to convert a Map to a
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
Scala newbie here Trying (1).+(2) returns a Int value of 3, so far so
I am a newbie Scala developer trying to figure out how to design a
well, I'm a newbie to both SBT and Scala, and I'm trying to create
Can anyone help this Scala newbie? Previously, we summed a number of quantities in
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
Scala programmer should have known that this sort of writing : class Person{ var

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.