[erlang-questions] Second order function lists:map

Rob Charlton <>
Tue Mar 10 16:24:47 CET 2009


Is this what you are after?

paa(A) ->
    fun(L) ->
            lists:map(fun(F)->
                              F(A)
                      end, L)
    end.

7> G = test:paa(42).
#Fun<test.0.118673008>
8> G([fun math:sin/1, fun math:cos/1, fun math:tan/1]).
[-0.916522,-0.399985,2.29139]


Cheers

Rob

Franco Milicchio wrote:
> Hi everyone!
>
> I am trying to write two small functions employing the lists package, 
> and now I am wondering how to proceed. My functions are quite 
> straightforward: I want two second-order functions that "abstract" 
> lists:map.
>
> The first one is a function that applies a given function to a list of 
> arguments. So, a first application gives me a function, and this 
> applied to a list gives me the mapping of my function over the list. I 
> have succeeded in doing this function and it seems to work:
>
> paa(F) ->
>     fun(L) ->
>             lists:map(F, L)
>     end.
>
> 41> PP=p:paa(fun math:sin/1).
> #Fun<p.1.71425429>
>
> 42> PP(lists:seq(0,100)).
> [0.0,0.8414709848078965,0.9092974268256817,
>  0.1411200080598672,-0.7568024953079282,-0.9589242746631385,
>  -0.27941549819892586,0.6569865987187891,0.9893582466233818,
>  0.4121184852417566,-0.5440211108893699,-0.9999902065507035,
>  -0.5365729180004349,0.4201670368266409,0.9906073556948704,
>  0.6502878401571169,-0.2879033166650653,-0.9613974918795568,
>  -0.750987246771676,0.14987720966295234,0.9129452507276277,
>  0.836655638536056,-0.008851309290403876,-0.8462204041751706,
>  -0.9055783620066239,-0.13235175009777303,0.7625584504796028,
>  0.956375928404503,0.27090578830786904|...]
>
>
> The second one is tricky to me (I am not an erlang guru!). I want its 
> "dual", so given a list of functions, I'd like to map them to a single 
> argument, writing a second-order function like the above p:paa/1. The 
> only problem is that I am stopped by this problem, I know it's easy 
> but I am not that good at functional programming :)
>
> Thanks for any help!
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions


-- 
Rob Charlton
Savage Minds Ltd





More information about the erlang-questions mailing list