About the | operator in erlang.

放肆的年华 提交于 2019-12-31 01:58:04

问题


We can make a nested list in erlang by writing something like this:

 NL = [[2,3], [1]]. 
 [[2,3],[1]]

But assume we wrote it like this instead:

 OL = [[2,3]|1].
 [[2,3]|1]

Is OL still a list? Can someone please elaborate more what OL is?


回答1:


This is called an improper list and should typically not be used. I think most library functions expects proper lists (e.g. length([1|2]) throws bad argument exception). Pattern matching with improper lists works though.

For some use cases, see Practical use of improper lists in Erlang (perhaps all functional languages)




回答2:


More information about | and building list is given in Functional Programming: what is an "improper list"? .



来源:https://stackoverflow.com/questions/20164356/about-the-operator-in-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!