[erlang-questions] Funargs: Ruby-like blocks for Erlang

Richard O'Keefe ok@REDACTED
Fri Jul 22 01:28:51 CEST 2011


On 22/07/2011, at 8:01 AM, Tristan Sloughter wrote:

> A reason I end up with far more anonymous functions in my Erlang than Haskell code (and end up with longer functions in Erlang) is the lack of 'let' and 'where'.
> 
> In Erlang I'd do:
> 
> -spec area_of_circles(record(circle)) -> [float()]
> area_of_circles(Circles) ->
>   lists:map(fun(C) -> 
>                               math:pi() * math:pow(C#circle.radius, 2) 
>                 end, Circles).
> 
Wouldn't you write

area_of_circles(Circles) ->
	[math:pi()*math:pow(C#circle.radius, 2) || C <- Circles].

> While in Haskell:
> 
> area_of_circles :: [Shape] -> [Float]
> area_of_circles circles =
>   L.map (area_of_circle) circles
>   where
>     area_of_circle (Circle radius) = pi * radius^2

Wouldn't you write

area_of_circles circles = [pi*r^2 | Circle r <- circles]

The problem isn't a lack of 'let' and 'where'.
That's solved just by making names distinct.
The problem is that normal function definitions don't nest;
while you can do

area_of_circles(Cs) ->
    Area = fun (C) -> math:pi()*math:pow(C#Circle.radius, 2) end,
    lists:map(Area, Cs).

you still have to use a 'fun' to do it.




More information about the erlang-questions mailing list