Higher Order Function Question

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

Higher Order Function Question

Hakan Stenholm
>Richard,
>
>Thanks you for the list comprehension suggestion. I had not thought of that
>(it does not exist in the Lisp world where I come from).
>
>I do use complement a lot, so I think, for me, it is worth the definition --
>but perhaps if I re-examine my approach, list comprehensions may obviate the
>need.
>
>As for
>4> apply({attr, isPk}, [A1]).
>
>as I mentioned, I was just testing for eventual use in filter, and as far as
>I know, in filter (and map, etc.) you *must* use the form {attr, isPk}. I
>always get errors when I try attr:isPk within map, filter etc.
>
>150> lists:map(attr:varName, rel:attrList(rel:findNamed(ng,"Member"))).
>** exited: {{badexpr,':'},[{erl_eval,expr,3}]} **
>

lists:map({attr,varName}, rel:attrList(rel:findNamed(ng,"Member"))).
should work, see examples below:
1>  lists:map({lists,append},[[[1],[2],[3]], [[b],[c]]]).
[[1,2,3],[b,c]]
3> {lists,append}([[1,2,3],[a,b,c]]).
[1,2,3,a,b,c]

There are three basic ways to define a fun:
Fun = fun(Arg1, Arg2 ....) -> ... end  % anynomouse fun
Fun = fun FunctionName/Arity           % local fun in current file
Fun = {Module, FunctionName}           % any exported function can be used

all which can be called as Fun(A1,A2, ....) as can be seen below:

2> fun(L) -> lists:append(L) end([[1,2,3],[a,b,c]]).
[1,2,3,a,b,c]

--------------------------------------
-module(test).
-export([test/0,
         test_call/0]).

head([A|_]) -> A.

test_call() ->
    fun head/1([1,2,3,4]).
---------------------------------------  
1> test:test_call().
1
   
Ofcourse we usally write something like:
Fun = ....
Fun(A1,A2, ....)  

 



Reply | Threaded
Open this post in threaded view
|

Higher Order Function Question

Alex Peake
Thank you Haken,

but the issue is not "apply" - I was only using that to experiment -- the
issue is "map" or "filter" which, apparently, will only accept {attr, isPk}
and not attr:isPk (which I am told is "more elegant" and certainly more
efficient).

map and filter will accept:

lists:map(fun(Arg) -> attr:varName(Arg) end,
rel:attrList(rel:findNamed(ng,"Member"))).

but this is quite ugly looking code (albeit probably more efficient than
{attr, isPk}...


Alex


> -----Original Message-----
> From: owner-erlang-questions
> [mailto:owner-erlang-questions]On Behalf Of Hakan Stenholm
> Sent: Friday, May 24, 2002 9:24 AM
> To: erlang-questions
> Cc: apeake
> Subject: RE: Higher Order Function Question
>
>
> >Richard,
> >
> >Thanks you for the list comprehension suggestion. I had not
> thought of that
> >(it does not exist in the Lisp world where I come from).
> >
> >I do use complement a lot, so I think, for me, it is worth the
> definition --
> >but perhaps if I re-examine my approach, list comprehensions may
> obviate the
> >need.
> >
> >As for
> >4> apply({attr, isPk}, [A1]).
> >
> >as I mentioned, I was just testing for eventual use in filter,
> and as far as
> >I know, in filter (and map, etc.) you *must* use the form {attr, isPk}. I
> >always get errors when I try attr:isPk within map, filter etc.
> >
> >150> lists:map(attr:varName, rel:attrList(rel:findNamed(ng,"Member"))).
> >** exited: {{badexpr,':'},[{erl_eval,expr,3}]} **
> >
>
> lists:map({attr,varName}, rel:attrList(rel:findNamed(ng,"Member"))).
> should work, see examples below:
> 1>  lists:map({lists,append},[[[1],[2],[3]], [[b],[c]]]).
> [[1,2,3],[b,c]]
> 3> {lists,append}([[1,2,3],[a,b,c]]).
> [1,2,3,a,b,c]
>
> There are three basic ways to define a fun:
> Fun = fun(Arg1, Arg2 ....) -> ... end  % anynomouse fun
> Fun = fun FunctionName/Arity           % local fun in current file
> Fun = {Module, FunctionName}           % any exported function can be used
>
> all which can be called as Fun(A1,A2, ....) as can be seen below:
>
> 2> fun(L) -> lists:append(L) end([[1,2,3],[a,b,c]]).
> [1,2,3,a,b,c]
>
> --------------------------------------
> -module(test).
> -export([test/0,
> test_call/0]).
>
> head([A|_]) -> A.
>
> test_call() ->
>     fun head/1([1,2,3,4]).
> ---------------------------------------
> 1> test:test_call().
> 1
>
> Ofcourse we usally write something like:
> Fun = ....
> Fun(A1,A2, ....)
>
>
>
>