user-defined operators

Samuel Rivas <>
Thu Mar 25 20:08:05 CET 2004


Eric Newhuis wrote:

> Yes good point.  foreach is a simple one.  The others like foldl always 
> throw me.  I am always going back to the man pages.  K's foldl operator 
> is just a single forward slash ("/").  It is pronounced "over".
> 
> F / Vector is read as "F over Vector".
> 
> Example:  Accumulate sum over a vector of integers:
> 
> K:
> +/1 2 3 4 5
> 
> Erlang:
> lists:foldl (fun (X, A) -> X + A end, 0, [1, 2, 3, 4]).
> 
> Would be nice to just say this in Erlang:
> 
> 0 + over [1, 2, 3, 4].
> 
  I find more readable

    Sum = fun(X, A) -> X + A end,
    lists:foldl(Sum, 0,  [1, 2, 3, 4]).

  If you don't know anything about the + over you can 'assume' that is
for adding the left side to each element of the right side, to fold the
right side starting in 0 using + or many other strange things like
mapping the left side in each element of the right side.

  When you found a operator you haven't seen before you must look for it
in the documentation too, but is reasonable to think that foldl will do
what it really does.

  What I mean is that "strange" operators are, at least, as confusing as
unknown functions, but a function can explain more or less what is
intended for. Overloading operators (beyond the overloading of
arithmetical operators to work either with naturals or reals) is too
deceptive, in my opinion. I saw me once thinking about "2" + "2" will
yield "4" or "22" or an error or ...

-- 
 ---------------------
|	Samuel        |
 ---------------------



More information about the erlang-questions mailing list