I'm both fairly new to learning programming, and fairly new to Scala. (2 semesters of CS and ~20 hours of independent study / practice.)
I recently started working through Functional Programming in Scala (amazon link here) and in the third chapter it wants us to work with the following simplification of the List data type in Scala, presumably to simultaneously become more comfortable with traits, classes, and polymorphic types while we learn how to work with data structures.
I put the following code into Eclipse IDE's Scala Worksheet, and then spent about two hours trying different ways to make it
- compile without errors, and
- display evaluated results on the righthand side.
I'm still struggling a lot with (2).
package fpinscala.datastructures
// The following three lines define the simplified List type and behavior
sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]
// The companion object List defines basic List operations
object List {
def sum(ints: List[Int]): Int = ints match {
case Nil => 0
case Cons(x,xs) => x + sum(xs)
}
def product(ds: List[Double]): Double = ds match {
case Nil => 1.0
case Cons(0.0, _) => 0.0
case Cons(x,xs) => x * product(xs)
}
def apply[A](as: A*): List[A] =
if (as.isEmpty) Nil
else Cons(as.head, apply(as.tail: _*))
}
When I run the code above, it outputs a standard error saying that a main argument is missing. Though I'm not very clear about how the def main(args: Array[String) {} or object some_object extends App {} actually work, I do know that they allow the code to actually compile and run. At the same time, the Scala Worksheet never required that I have a Main method in the past, though in the past I also wasn't using classes or traits in the same file. I know I must be missing something.
I've tried
- extending the List object with App
- or adding a def main argument underneath it
- creating an object called "Classes" for the sealed trait and adding the main argument to it
- or extending it with App
- creating a larger object "Worksheet" that I then added all of the following to under a def main argument
- or extending "Worksheet" with App
- in another Eclipse IDE file, creating a New Scala Trait, and adding the trait and class code in it, and then trying to import it into the worksheet, realizing that the companion object List needs to exist in the same file
Some of these have compiled, others have not, but none of them so far have resulted in the worksheet actually producing evaluated results on the righthand side.
Maybe mixing in traits and companion classes into the worksheet is a lot more complicated than just using a worksheet with a singleton object, but I'd really like to use the worksheet to do my problems, so if there's any way to make this work, I'd love to know how. Also, I don't even think I'd know how to actually do anything without the worksheet (currently), so if it cannot do this inside the worksheet, can someone tell me how else I might work with the code?
Also, here's the exercise requiring the code above that I'm trying to work on:
Let’s try implementing a few different functions for modifying lists in different ways. You can place this, and other functions that we write, inside the List companion object.
3.2 Implement the function tail for removing the first element of a List. Note that the function takes constant time. What are different choices you could make in your implementation if the List is Nil? We’ll return to this question in the next chapter.
Thanks so much for your time.
Aucun commentaire:
Enregistrer un commentaire