Thursday, July 24, 2008

Scala Exercises

I have completed Tonny Morris Scala exercises. (maybe I'll put them on the svn as well, or maybe mercurial ?)...
Anyway some observations:
- it felt like doing the 'Little Schemer' in Scala.
- after doing 3-4 exercises, you feel the need to use/implement fold_left/fold_right.
- maximum is buggy: what's the maximum on an empty list? (the maximum found must be contained in the list;reasoning related to 'Noumenal Null'). I believe that this exercise is a 'tricky question' and instead of throwing an exception, or returning Nothing or Int.NaN, probably the signature should be changed to : maximum(x: List[Int]) : Option[Int]

- In the List's implementation of map/filter/... they use instead of pattern matching if x.isEmpty ... else ( do_something(x.head, x.tail). Probably it's faster than pattern decomposition.

1 comment:

Garrett Rowe said...

A couple of points. I don't believe there was a rule against using the built-in foldLeft or foldRight. As to the implementation of maximum on an empty list, it seems to me that throwing an error when calling maximum on an empty list is a perfectly reasonable thing to do, like calling head or reduceRight on an empty list