高阶函数中变量的绑定和匹配问题

阅读更多
一直一来没有留意到这个问题,今天偶尔在blog上看到,就标记一下,别让自己忘记了。

文章在 http://easyerl.blogspot.com/2007/10/high-order-functions-must-be-tested.html

测试代码如下:
1> Filter = fun(Elem) -> fun({Elem, _, _}) -> true; (_) -> false end end. 
#Fun
2> C = Filter(cpu).
#Fun
3> C({test, t, t}).
true % this should have been false ...


按理来说,C应该等同与一个这样的函数:
fun({cpu, _, _}) -> true; (_) -> false end.

这样
C({test, t, t}) 应该返回 false

作者指出,参考 http://www.erlang.org/doc/programming_examples/funs.html#2.4,有一段如下文字:
引用
The rules for importing variables into a fun has the consequence that certain pattern matching operations have to be moved into guard expressions and cannot be written in the head of the fun.


因此应该用
f(...) ->
    Y = ...
    map(fun(X) when X == Y ->
             ;
           (_) ->
             ...
        end, ...)
    ...

取代
f(...) ->
    Y = ...
    map(fun(Y) ->
             ;
           (_) ->
             ...
        end, ...)
    ...


因此正确做法是
Filter = fun(Elem) ->
 fun({X, _, _}) when X == Elem -> true; (_) false end
 end.

你可能感兴趣的:(Erlang,C,C++,C#,F#)