|
![]() |
||||||||||||||
Program Structure TutorialPartial ApplicationPartial application assumes application of a function to incomplete argument set. In many functional programming languages partial application is implemented by specifying only the beginning of the argument list, in the respected order. Such approach is called curried syntax. The result of a curried partial application is a function that maps the remaining arguments to the result. For example, in programming language Haskell, if we assume that functions double and inc are defined as: double f x = f (f x) inc x = x + 1 we could write function add2, which adds 2 to the argument, as: add2 = double inc Programming language Wafl supports a different kind of partial application. It is always necessary to specify as much arguments as the function expects, but some of the arguments may remain void. Void arguments are specified by underscore symbol _. Each of function arguments may be void, and any number of arguments may be void. (However, there is no reason to use partial application with all arguments void, because it is simpler to use only the function name). The result of a partial application is a function. Its arguments correspond to void arguments of the partial application. In the following example, few partial applications are presented. Source code:{# strGetFirst5a("1234567890"), strGetFirst5b("1234567890"), charAt(1), add2(5), // following expressions evaluate the same value subStr("1234567890",5,3), subStr(_,5,_)(_,3)("1234567890") #} where{ // the following definitions are the same, // representing functions that extract // the first 5 characters of a string strGetFirst5a(s) = strLeft(s,5); strGetFirst5b = strLeft(_,5); // charAt(n) returns the n-th character of "example" charAt = subStr( "example", _, 1 ); // the same example as in Haskell add2 = double( inc, _ ); inc(x) = x + 1; double(f,x) = f(f(x)); } Result:{# "12345", "12345", "x", 7, "678", "678" #}
|
|
||||||||||||||
© 2006 Saša Malkov | |||||||||||||||