[erlang-questions] View patterns

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Thu Jul 26 10:04:37 CEST 2007


Hmph! Some parentheses were also missing in
my transcription. Luckily, I received the Erlang
book yesterday, so I can read up how to do basic
Erlang programming...

I've attached my parse transform. Compiling with
'P' gives a good idea of what transformation is 
done. It only handles patterns in function clauses,
and doesn't attempt to reduce repeated work.
It's just intended as a starting point for some
experimentation.

== Example

-module(test)
-compile({parse_transform, vp_expand}).
-export([factorial/1]).


np(K, X) when integer(X), integer(K), X >= K ->
    {X-K};
np(_, _) ->
    false.

factorial(0) -> 1;
factorial(_:_(np,[1], {N})) ->
    (N+1)*factorial(N).

==

The nice thing about the _:_(...) pattern
is that it's syntactically valid, but wholly
illegal in all contexts. If you forget to do
the parse transform, the code will not compile. (:

BR,
Ulf W


> -----Original Message-----
> From: erlang-questions-bounces@REDACTED 
> [mailto:erlang-questions-bounces@REDACTED] On Behalf Of ok
> Sent: den 26 juli 2007 05:13
> To: Erlang Questions
> Subject: Re: [erlang-questions] View patterns
> 
> I made a mistake.
> 
>      np(K, X) when integer(X), integer(K), X >= K ->
>          {X};              %%%% HERE
>      np(_, _) ->
>          false.
> 
> should of course be
> 
>      np(K, X) when integer(X), integer(K), X >= K ->
>          {X - K};
>      np(_, _) ->
>          false.
> 
> Fix that and all is well.
> 
> The real trick with view patterns is to compile them so that 
> you do not do too much repeated work.
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vp_expand.erl
Type: application/octet-stream
Size: 6669 bytes
Desc: vp_expand.erl
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070726/8072a506/attachment.obj>


More information about the erlang-questions mailing list