5.1,5.2节 anonymous functions, curried functions
- 章节名:5.1,5.2节 anonymous functions, curried functions
- 2012-09-20 17:25:05
The most powerful techniques of functional programming are those that treat function as data. Most functional languages give function values full rights, free of arbitrary restrictions. Like other values,functions may be arguments and results of other functions and may belong to pairs,lists and trees. A function is higher-order (or a functional) if it operates on other functions. For instance, the functional map applies a function to every element of a list,creating a new list. Functions as values: Functions in ML are abstract values; they can be created; they can be applied to an argument; they can belong to other data structures.Nothing else is allowed. 5.1 Anonymous functions with fn notation An ML function need not have a name. If x is a variable (of type a) and E is an expression (of type t) then the expression fn x => E denotes a function of type a -> t . Its argument is x and its body is E. Pattern-matching is allowed : the expression fn P1 => E1 | ... | Pn => En denotes the function defined by the patterns P1,...,Pn. It has the same meaning as the let expression let fun f(P1) = E1 | ... | f(Pn) = En in f end; provided f does not appear in the expressions E1,...,En. For example. fn n=> n*2 is a function that doubles an integer. It can be applied to an argument; it can be given a name by a val declaration.
(fn n => n*2)(9); > 18 : int val double = fn n => n*2; > val double = fn : int -> int5.2 Curried functions Currying is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application). For example :
fun prefix pre = let fun cat post = pre^post in cat end;Using fn notation,and Currying transform it to:
fn pre => (fn post => pre ^ post)A curried function permits partial application. Applied to its first argument (of type a1) its result is a function of type a2 -> t. Curried functions may be recursive . Calling replist n x makes the list consisting of n copies of x:
fun replist n x = if n =0 then [] else x :: replist (n-1) x; > val replist = fn : int -> 'a -> 'a list replist 3 true; >[true,true,true] : bool listRecursion works by the usual evaluation rules , even with currying . The result of replist 3 is the function fn x => if 3=0 then [] else x :: replist (3-1) x Applying this to true produces the expression true :: replist 2 true As evaluation continues, two further recursive calls yield true :: true :: true :: replist 0 true
mr x对本书的所有笔记 · · · · · ·
-
第四章 4.10 二叉树
ML 用datatype可以递归定义一个二叉树类型: (* Lf 代表叶子节点,Br 为包含当前节点,左子树...
-
Tree-based data structures
此节用二叉树实现了:字典(Dictionany), 数组(Array); 用二叉树实现这两个结构的优点:(...
-
5.1,5.2节 anonymous functions, curried functions
-
5.4 functions as arguments and results
The sorting functions of Chapter 3 are coded to sort real numbers. They can be generali...
-
General-purpose functionals
Functional programmers often use higher-order functions to express programs clearly and...
说明 · · · · · ·
表示其中内容是对原文的摘抄