Shadowed head variable in fun

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Shadowed head variable in fun

WILLIAMS Dominic
Hello,

How does one 'consume' a head variable in a fun?

symbol(Char) when integer(Char) ->
        fun([Char|Rest]) -> [{Rest,Char}];
           (Other) -> []
        end.

This compiles with a Warning: variable 'Char' shadowed in 'fun'.
And doesn't do what I want...

i.e.

F = symbol($a),
F("Hello").

returns [{"ello",72}] (I want it to return []).

Thanks for your help,

Dominic.


Reply | Threaded
Open this post in threaded view
|

Shadowed head variable in fun

Ulf Wiger-4

The Char in the fun's function head is not the same as Char
in the surrounding scope (thus the warning). The simplest
way to amend your implementation is probably:

symbol(Char) when integer(Char) ->
   fun([C|Rest]) when C == Char -> [{Rest,Char}];
      (Other) -> []
   end.

I haven't verified absense of typos in the above code, but
you probably get the idea.

/Uffe

On Fri, 23 May 2003, WILLIAMS Dominic wrote:

>Hello,
>
>How does one 'consume' a head variable in a fun?
>
>symbol(Char) when integer(Char) ->
> fun([Char|Rest]) -> [{Rest,Char}];
>   (Other) -> []
> end.
>
>This compiles with a Warning: variable 'Char' shadowed in 'fun'.
>And doesn't do what I want...
>
>i.e.
>
>F = symbol($a),
>F("Hello").
>
>returns [{"ello",72}] (I want it to return []).
>
>Thanks for your help,
>
>Dominic.
>

--
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson AB, Connectivity and Control Nodes