1 つの方法は次のようになります。
List = [ true, true, true, true, false, true, true, false, true ],
{_, Mode} = lists:foldr(
fun(A, {M, S}) -> {M * 2, (A * M) + S } end,
{1, 0},
[case X of true -> 1; false -> 0 end || X <- List]
).
Mode =:= 8#755.
true
io:format("~.8B~n", [Mode]).
755
ok
またはモジュールとして:
-module(change_mode_utils).
-export([bool_list_to_mode/1]).
-spec bool_list_to_mode(List::[boolean()]) -> integer().
bool_list_to_mode(List) when is_list(List) ->
{_, Mode} = lists:foldr(
fun(true, {M, S}) -> { M * 2, M + S };
(false, {M, S}) -> { M * 2, S } end,
{1, 0},
List
),
Mode.
証拠
1> c(change_mode_utils).
{ok,change_mode_utils}
2> change_mode_utils:bool_list_to_mode(
[ true, true, true, true, false, true, true, false, true ]
) =:= 8#755.
true