Is there any function in erlang to check if the list is a proplist? [duplicate]

StackOverflow https://stackoverflow.com/questions/23333997

  •  10-07-2023
  •  | 
  •  

Question

I have a data in format of tuple :

{data, [[{a, 2}, {b, 3}], [{x, 1}, {v,2}], [1,2,3,4], "hello world", 1111]}

Since,

{data, [[{a, 2}, {b, 3}], [{x, 1}, {v,2}], [1,2,3,4], "hello world", 1111]} = {data, L}.

And need to check if the element of L is a proplist, like here : [{a, 2}, {b, 3}] and [{x, 1}, {v,2}] are proplists.

Is there any function in erlang to check whether the list is proplist?

is_proplist(List) should return true or false

Was it helpful?

Solution

The function F1 below checks for proplists (assuming that a single atom 'a' is equivalent to {'a',true})

1> F = fun(X) when is_atom(X) -> true; ({X,_}) when is_atom(X) -> true; (_) -> false end.
#Fun<erl_eval.6.80484245>
2> L = [[{a, 2}, {b, 3}], [{x, 1}, {v,2}], [1,2,3,4], "hello world", 1111].
[[{a,2},{b,3}],[{x,1},{v,2}],[1,2,3,4],"hello world",1111]
3> F1 = fun(X) when is_list(X) -> lists:all(F,X); (_) -> false end.
#Fun<erl_eval.6.80484245>
4> [X || X <- L, F1(X)].                                           
[[{a,2},{b,3}],[{x,1},{v,2}]]
5> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top