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?
First, I would not reinvent the wheel… the method max already is defined in
RichInt, so you can writea max b, foraandbintegers.ALso,instead ofsliceis deprecated, thereforelst.slice(0, num)I would uselst.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 ofslice(Int), which is equivalent todrop(Int). I still findlst.take(num)to be clearer thanlst.slice(0, num):).(nitpick) Your last line does also not compile as you forgot to add
Nilto the end of your sequence of cons.1::2::3::4, would end up invoking::on anInt, which does not have this method. That’s why you need to addNilto the end (invoke::onNil).Also, the algorithm you have used is not obvious at the first glance. The way I would write this is as follows:
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(_ > _).headout 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 writeproductas the following, to be more explicit:-- Flaviu Cipcigan