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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:39:16+00:00 2026-05-12T15:39:16+00:00

I am a Scala newbie, just starting to learn the language. I solved Problem

  • 0

I am a Scala newbie, just starting to learn the language.

I solved Problem 8 from Project Euler page.

The code looks like this (I removed all the code to do with reading of an input file):

def max(n1: Int, n2: Int): Int = Math.max(n1, n2)

def max_product(digits: List[Int], num: Int): Int = {
    def max_core(lst: List[Int], curr_max: Int): Int = lst match {
        case a if lst.length >= num => 
            max_core(a.tail, max(lst.slice(0, num).reduceLeft(_*_), curr_max))
        case _ => curr_max
    }

    max_core(digits, 0)
}

println(max_product(1::2::3::4::2::3::Nil, 2))

It works fine, the result is correct. However, I am not completely satisfied with this solution. I don’t like the max_core sub-function and have the feeling it can be improved. My understanding of FP is, you should iterate over a list, slicing seems to not be the way here.

The question is: how?

  • 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-12T15:39:17+00:00Added an answer on May 12, 2026 at 3:39 pm

    First, I would not reinvent the wheel… the method max already is defined in RichInt, so you can write a max b, for a and b integers.

    ALso, slice is deprecated, therefore instead of lst.slice(0, num) I would use lst.take(num). Deprecated methods will probably be gone when Scala 2.8 is launched.

    EDIT: Indeed, as Daniel pointed out, slice(Int, Int) is not deprecated. I was quite in a hurry when I initially wrote this, and I was thinking of slice(Int), which is equivalent to drop(Int). I still find lst.take(num) to be clearer than lst.slice(0, num) :).

    (nitpick) Your last line does also not compile as you forgot to add Nil to the end of your sequence of cons. 1::2::3::4, would end up invoking :: on an Int, which does not have this method. That’s why you need to add Nil to the end (invoke :: on Nil).

    Also, the algorithm you have used is not obvious at the first glance. The way I would write this is as follows:

    val numbers = /*"--the string of numbers--"*/.map(_.asDigit).toList
    
    def sliding[A](xs: List[A], w: Int): List[List[A]] = {
      for(n <- List.range(0, xs.size - w)) 
        yield xs drop n take w
    }
    
    def product(xs: List[Int]): Int = (1 /: xs) (_ * _)
    
    sliding(numbers, 5).map(product).sort(_ > _).head
    

    I feel that the last line explains quite well what the algorithm is supposed to do – take a sliding window of the list, calculate the product in that sliding window and then get the maximum of the calculated products (I have implemented the maximum function as sort(_ > _).head out of laziness, I could have done something O(n) rather than O(n log(n)) if performance was critical… it still runs under a second though).

    Note that the sliding function will be in the Scala 2.8 library (see Daniel’s post, from where I was inspired in writing this definition of sliding).

    EDIT: Oops… sorry about the /:. I just like the conciseness of it and the fact that the initial element of the fold comes before the list. You could equivalently write product as the following, to be more explicit:

    def product(xs: List[Int]): Int = xs.foldLeft(1)(_ * _)
    

    -- Flaviu Cipcigan

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

Sidebar

Related Questions

Scala Newbie alert: basically I'm trying to do something like this: where I pattern
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
Scala newbie here, I just downloaded Eclipse 3.6.2 and Scala IDE 2.0.0-beta4 (with Scala
scala.math.ScalaNumber is a Java file which looks like this: public abstract class ScalaNumber extends
In my Scala code, I am fetching a response from a server using the
While playing around with regexps in Scala I wrote something like this: scala> val
I just finished reading a book on scala. What strikes me is that every
I have an application where I would like to have mixed Java and Scala
I'm a newbie to both R and LaTeX and have just recently found how

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.