ITeye博客

erlang入门系列(2) if else

nid007 · 2010-10-09 15:04 · erlang · 编程语言 · Markdown

事实上erlang没有else这个关键词


#!/usr/bin/env escript

main(Args) ->
	[t,t2|_]=Args,	
	Name=list_to_integer(T),
	Name2=list_to_integer(T2),
	if
		Name==1;Name2==1 ->
			io:format("first match ~n");
		Name==2,Name2==2 ->
			io:format("second match~n");
		true ->
			io:format("value=~s ~s ~n",Args)
	end.

把上面的代码保存为ifelse.ers,然后可以运行了 G:\erl>escript.exe ifelse.ers escript: exception error: no match of right hand side value --因为没有传递参数,所以模式匹配失败了。 G:\erl>escript.exe ifelse.ers 1 2 first match

G:\erl>escript.exe ifelse.ers 2 2 second match

G:\erl>escript.exe ifelse.ers 3 2 value=3 2

上面的逻辑判断“,”可以理解为 and,";"可以理解为or 但是对于一些复杂逻辑用erlang怎么写我还不知道,比如下面的java代码怎么转成erlang的


(A==1 || B==2) && (C==3 || D==4)

如果你知道 还请留言,谢谢!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。