Seb*_*itz 2 erlang list append
我有一个空列表,并且在特殊情况下,想要将一个元组追加到列表中.我怎样才能做到这一点?
我尝试了几种方法:
case ReqFilePath of
"style.css" ->
ResponseHeaders = [{"Content-Type", "text/css"}];
_Else ->
ResponseHeaders = []
end,
case filelib:is_file(File) of
true ->
{ok, Content} = file:read_file(File),
{output, Content, ResponseHeaders}; % Complains ResponseHeaders is not safe
false ->
not_found
end.
Run Code Online (Sandbox Code Playgroud)
这也不起作用,因为已经设置了变量.当我首先初始化ResponseHeaders = []时,然后尝试向其添加值
ResponseHeaders = lists:append(ResponseHeaders, [{"Content-Type", "text/css"}]);
Run Code Online (Sandbox Code Playgroud)
我得到一个匹配错误.你如何在Erlang中正常做到这一点?
在erlang中执行此操作的常用方法是使用另一个变量:
ResponseHeaders = [{"Content-Type", "text/css"}],
[...]
ResponseHeaders2 = ResponseHeaders ++ [{new_thing}]
Run Code Online (Sandbox Code Playgroud)
或者您可以创建一个为您构建ResponseHeaders的函数:
ResponseHeaders = build_headers(ReqFilePath).
build_headers("skin.css") -> [{"Content-Type", "text/css"}];
build_headers(_) -> [].
Run Code Online (Sandbox Code Playgroud)
不要忘记改变你的心态到二郎的方式!;)
| 归档时间: |
|
| 查看次数: |
2503 次 |
| 最近记录: |