Erlang Mailing Lists

Author Message

<  Erlang  ~  Newbie question about functions returning functions

ivan
Posted: Wed Oct 03, 2007 11:58 pm Reply with quote
Joined: 03 Oct 2007 Posts: 2
Hello,

I am trying to define a function which returns a function and am running into trouble, perhaps someone can help with advice?

What I want to do is write a function -- let's call it remove_match -- with a single argument which is a matching function. So I want to be able to say something like:

Code:

MyTest = fun (X) -> X =:= 2 end.
X = remove_match(MyTest).

And the result of X([1, 2, 3]) should be [1, 3].

So I attempt to write something like the following:

Code:
remove_match (Test) ->
  fun
    (_A, []) -> [];
    (A, [H|T]) ->
      case Test (A, H) of
        true -> <...> (A, T);
        false -> [H|<...> (A, T)]
      end
    end.


but I can't quite figure out what to put in place of the <...> above.

Is there any easy solution to my problem? Am I trying to do something impossible? What is the most appropriate, Erlang idiom for my problem?

Thanks for any help,

--
ivan
View user's profile Send private message
Mazen
Posted: Thu Oct 04, 2007 8:38 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
I'm not sure I understand your code but I think I can describe to you what you need.

Assume:
Code:

test() ->
    Fun = fun() -> io:format("Fun print out~n") end,
    Fun(),
    AnotherFun = fun(X) -> io:format("Fun print: ~p~n",[X]) end,
    AnotherFun("Hello!").


so if you define a fun and assign it to a variable (either directly or through a function head match) when you can use that function by using the variable directly. I.e. if you a fun as a variable, you just pass around that variable as a normal variable.

Side Note Tip: Don't nest too many funs, and try not to pass around to many funs everywhere. It is confusing and nobody will like you Smile I've experienced that recently where we had someone who made 2 nested funs which he was passing around as an argument all over the place and it is just horrible Very Happy

Hope this helped?!
View user's profile Send private message
ivan
Posted: Fri Oct 05, 2007 11:42 pm Reply with quote
Joined: 03 Oct 2007 Posts: 2
Thanks for the reply. Actually, I figured out how to do it, I had to enclose the call to remove_match in parentheses. So this is how it works:
Code:

[remove_match (Test) ->
  fun
    (_A, []) -> [];
    (A, [H|T]) ->
      case Test (A, H) of
        true -> (remove_match (Test)) (A, T);
        false -> [H|(remove_match (Test)) (A, T)]
      end
    end.

So now if you do
Code:

T = fun (X, Y) -> X =:= Y end.
Z = remove_match(T).
Z(2, [1, 2, 3, 4]).

the result is [1,3,4].

I am confused about why do you advise against returning functions from functions? Once you get used to it, I think it expands enormously the expressive power of the language, don't you think? I find this to be one of the best features of Erlang.

Regards,

--
ivan
View user's profile Send private message
Mazen
Posted: Mon Oct 08, 2007 8:40 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
ivan wrote:

I am confused about why do you advise against returning functions from functions? Once you get used to it, I think it expands enormously the expressive power of the language, don't you think? I find this to be one of the best features of Erlang.


Well, we are not disagreeing on the usefulness and I think it is very powerful. However, as much as it is powerful it also complicates the code alot!

My simple point is: If you use anonymus funs, which uses funs, that return a funs and run funs from "somewhere", It is very easy to get it wrong for people that are going to debug your code. Smile

But I do agree, they are powerful, just don't overdo it is my advise. Keep it as simple as possible Smile
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum