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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:42:45+00:00 2026-06-05T01:42:45+00:00

From Programming in Scala ,we know that foreach is a high-order function which takes

  • 0

From Programming in Scala,we know that foreach is a high-order function which takes a procedure with return type Unit.So I think the following slice would work:

val abcde = List("a","b","c","d","e")
abcde.foreach(print _.toUpperCase)

however it tells me that:

1: error: ')' expected but '.' found.
  abcde foreach (println _.toUpperCase)
                          ^

But these two below both work well:

println("abcde".toUpperCase)
abcde.foreach(print _)

So what’s the difference?

  • 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-05T01:42:47+00:00Added an answer on June 5, 2026 at 1:42 am

    These two are using _ in different ways:

    abcde.foreach(print _.toUpperCase)
    abcde.foreach(print _)
    

    In the first case, you have an anonymous function where _ denotes a placeholder for a parameter.

    In the second case, _ means you’d like a function value for the method print (an eta expansion).

    So comparing the two is irrelevant.

    More to the point would be this:

    scala> print "abcde".toUpperCase
    <console>:1: error: ';' expected but string literal found.
           print "abcde".toUpperCase
                 ^
    

    As you can see, this doesn’t work, so replacing "abcde" with _ wouldn’t work either.

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

Sidebar

Related Questions

I don't understand why authors said that Code Listing 9.1 from Programming in Scala
I'm having difficulty figuring out how to make the jump from a Scala high-order
From reading parts of the Programming in Scala book, I realize that Scala can
From the An Overview of the Scala Programming Language, Second Edition : // Scala
I don't understand the last line of this function from Programming Perl 3e .
I am from procedure programming background, end up writing TSQL recently with my new
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
I recently getting to know about functional programming (in Haskell and Scala). It's capabilities
From the Programming in Scala (second edition) , bottom of the p.98: A balanced
In the book Programming in Scala from Martin Odersky there is a simple example

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.