[erlang-questions] Intel Quad CPUs
Hugh Perkins
hughperkins@REDACTED
Tue Sep 4 16:04:27 CEST 2007
An anonymous reader writes:
> I'm probably missing the point here (I'm new, be gentle), but what
> would you have it return for functions that take a multitude of (same
> arity, yet) wildly varying inputs; something like the following
> contrived function?
>
> f([]) -> [];
> f([H|T]) when H < 3 -> {low};
> f([A, B, C|Rest]) when C =:= {foo} -> [A|T];
> f(_) -> 42.
>
Yes, that is the challenge ;-)
I dont have a good answer, but to show that it's not impossible,
here's an example of one way of doing it:
f :: ( StudentNames );
f(StudentNames@[]) -> [];
f(StudentNames@[H|T]) when H < 3 -> {low};
f(StudentNames@[A, B, C|Rest]) when C =:= {foo} -> [A|T];
f(_StudentNames) -> 42.
Basically, we optionally can declare the parameter names using "::".
If we have chosen to declare the parameter names, then we must use the
same parameter name in each pattern matching, otherwise we get a
compilation error.
For unused parameters, we prefix the name with an underscore.
For compound parameters, such as lists, tuples or records, we use the
"@" notation.
More information about the erlang-questions
mailing list