perms([]) -> [[]]; perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
简单说明下防止以后再犯蠢 = =
首先 <- 是从表中取值,取的是值(所以这里是[H]),而不是把表本身给了前边的变量
先看下这个简单的示例:
1 2 3 4 5 6 7 8
71> f(). ok 72> L = "abc". "abc" 73> [[H|T] || H <- L, T <- L--[H]]. [[97|98],[97|99],[98|97],[98|99],[99|97],[99|98]] 74> [[H,T] || H <- L, T <- L--[H]]. ["ab","ac","ba","bc","ca","cb"]
很清楚了, <- 要从一个表中取到全部的值,也就是说在遍历完全部值之前不会停止
接着再说一下空列表时候必须给定值[[]]:
1 2 3 4 5 6
80> [[H|T] || H <- "abc", T <- []]. [] 81> ["a"|]. * 1: syntax error before: ']' 82> [[H|T] || H <- "abc", T <- [[]]]. ["a","b","c"]
demo(User) -> [catcher(I, User) || I <- [1,2,3,4,5]].
catcher(N, User) -> try generate_exception(N) of Val -> {N, normal, Val} catch Throw:X -> case User =:= developer of false -> {N, Throw, X}; true -> {X, erlang:get_stacktrace()} end end.