Search |
||
Know When to FoldPosted by cayhorstmann on October 5, 2008 at 8:26 PM PDT
I am teaching an undergraduate course in programming languages. We build interpreters and compilers for toy languages, in the hope that students gain a basic understanding of syntax, semantics, and language translation.
I am learning Scala on the job, and I don't know all of the language
features yet. In particular, surely there must be some kind of
The reason is that I try to follow the advice of the Scala gurus and use
def fac(n : Int) = { var r = 1; for (i <- 2 to n) r *= i ; r }
As L. Peter Deutsch said, to iterate is human; to recurse, divine: def fac(n : Int) : Int = if (n <= 1) 1 else n * fac(n - 1) Recursing may be divine, but it even better to fold: def fac(n : Int) = (1 /: (2 to n)) {_ * _}
To visualize what is going on, draw a tree: op
/ \
op 4
/ \
op 3
/ \
1 2
The
There is also a At first, I thought that these operators are mere curiosities, but they turned out to come in handy many times. Consider evaluating a block in a toy language: {
val a = 3;
val b = 2 * a;
val fun = { x => x * x };
...
}
Each definition has to be added to an environment (a mapping of names to values). Here is the tree: op
/ \
op third def.
/ \
op second def.
/ \
original first def.
environ.
What is the operation? To add the definition to the previous environment.
There is a function (env /: block.defs) { evalDef(_, _) }
![]() With functional languages, there are all these nifty idioms that lead to
very concise code that looks like random line noise to the uninitiated. (It
isn't just Scala—in Ruby, The conventional wisdom (for example, expressed in this blog) is that functional programming will never be mainstream. I used to think that too, but now I am having second thoughts. After all, if you now look at Pascal, it is hard to imagine why people were so opposed to it. It is just C with a saner syntax, or Java with only static methods and no objects. And Microsoft, surely the bastion of the blue-collar programmer, is adding all sorts of functional programming features to its flagship language. Check out this blog on folding with LINQ. It is pretty disconcerting—kind of like Sarah Palin's correctly pronouncing nu-cle-ar. Maybe we are at that point in history when closures are becoming mainstream? »
Comments
Comments are listed in date ascending order (oldest first)
Submitted by ijuma on Sun, 2008-10-05 21:52.
Hi,
Folds are indeed very useful. Before anyone complains about weird symbols in Scala, one should note that there are non-symbolic alternatives for both variants represented here, they are unsurprisingly named foldLeft and foldRight.
Regarding for/while loops, I am not sure if you were joking but Scala has while loops that behave like Java's and for/sequence comprehensions[1] that are more powerful than Java's for loop.
[1] http://www.scala-lang.org/node/111
Submitted by zero on Mon, 2008-10-06 00:02.
Hi, you also might be interested implementing fac with utilizing the 'Stream' class when you want more the just a single value:
http://gestalt.monoid.net/blog/2007/10/streams-for-incremental-and-tail....
|
||
|
|