[erlang-questions] Correct syntax of a case clause

Raimo Niskanen raimo+erlang-questions@REDACTED
Mon Jun 15 16:18:26 CEST 2009


On Sun, Jun 14, 2009 at 07:35:25PM +0100, Joel Reymont wrote:
> 
> On Jun 14, 2009, at 6:58 PM, info wrote:
> 
> >case Expr of
> >a,b -> block1;        % if Expr = a or b then block1
> >  c -> block2         % if Expr = c then block2
> >end
> >
> >(a,b) gives an error
> 
> As it should.
> 
> >What is the correct syntax ?

Two possibilities:

b1() ->
    block1.

case Expr of
    a -> b1();
    b -> b1();
    c -> block2
end

... or ...

sel(a) -> b1;
sel(b) -> b1;
sel(c) -> b2;

case sel(Expr) of
    b1 -> block1;
    b2 -> block2
end

... you get the idea!

> 
> 
> {a, b}
> 
> For example...
> 
> 1> foo:main().
> check({1,2}) = got_1
> check(3) = got_2
> ok
> 
> -module(foo).
> 
> -compile([export_all]).
> 
> check(X) ->
> 	case X of
> 		{_, _}	-> got_1;
> 		_	-> got_2
> 	end.
> 
> main() ->
> 	io:format("check(~p) = ~p~n", [{1, 2}, check({1, 2})]),	
> 	io:format("check(~p) = ~p~n", [3, check(3)]).
> 
> ---
> Mac hacker with a performance bent
> http://www.linkedin.com/in/joelreymont
> 
> 
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list