检查Erlang护卫员的会员资格 [英] Checking for membership in an Erlang guard

查看:135
本文介绍了检查Erlang护卫员的会员资格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Erlang中编写一个if语句的最简单的方法是什么,其中一部分是成员(E,L),即测试是否 E 是列表 L 的成员?天真的方法是:

  if 
... andalso member(E,L) - > ...
end

但是不行,因为如果我明白了, code> member 不是一个守卫表达式。

解决方案

如您所说,会员功能不是有效的护卫员。相反,您可以考虑使用案例模式?可能在case表达式中包含你的其他if-子句。

  case {member(E,L),Expr} 
{true,true} - > do(),is_member;
{true,false} - > IS_MEMBER;
{false,_} - > no_member
end


What is the simplest way to write an if statement in Erlang, where a part of the guard is member(E, L), i.e., testing if E is a member of the list L? The naive approach is:

if 
  ... andalso member(E,L) -> ...
end

But is does not work becuase, if I understand correctly, member is not a guard expression. Which way will work?

解决方案

Member functionality is, as you say, not a valid guard. Instead you might consider using a case pattern? It's possibly to include your other if-clauses in the case expression.

case {member(E,L),Expr} of
  {true,true} -> do(), is_member;
  {true,false} -> is_member;
  {false,_} -> no_member
end

这篇关于检查Erlang护卫员的会员资格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆