2.46. Procedure Values Again
We've seen the advantages of higher order
functions and procedures. It is possible
to define anonymous procedure and function values
literally, as in the next example,
they're called lambdas, after the lambda calculus:
Start felix section to tut/tutorial/tut-1.46-0.flx[1
/1
]
1: #line 4425 "./lpsrc/flx_tutorial.pak"
2:
3:
4: val f = fun(a:int):int = { return a * a; };
5:
6: print (f 1);
7: endl;
8:
9: print
10: (
11: (fun(a:int):int = { return a * a; })
12: 1
13: );
14: endl;
15:
16: proc thrice(p:unit->void) { p(); p(); p(); }
17: thrice ( proc() { print 3; endl; } );
18: thrice ( proc { print 3; endl; } );
19: thrice { print 3; endl; };
20: { print "finished"; endl; };
Start data section to tut/tutorial/tut-1.46-0.expect[1
/1
]
1: 1
2: 1
3: 3
4: 3
5: 3
6: 3
7: 3
8: 3
9: 3
10: 3
11: 3
12: finished
Mickey Mouse! Checkout the shortcut on the
second last line. You can just write
statements in curly brackets for an
anonymous procedure taking unit argument,
you can leave off the
proc() // or
proc
But why does the last line work?
The answer is: this is a special shortcut.
A statement consisting of an anonymous
procedure taking unit, and a semicolon ;
is a shortcut for a call:
{ print 1; }; // is a shortcut for ..
{ print 1; } ();
much the same as
endl; // is a shortcut for ..
endl ();
Now, remember those ugly semicolons at the end
of the while statement? Are you getting a glimmer?
Hint: Felix doesn't have a while statement.
It's a library routine!