[erlang-questions] Question about lists:filter
José Valim
jose.valim@REDACTED
Wed Aug 28 13:58:32 CEST 2013
In your problem4() function:
*problem4()->lists:max(lists:filter(isPal,cand())).*
*
*
isPal is an atom. It is simply a value. If you want to pass the function
isPal declared above as a function, you need to write:
*problem4()->lists:max(lists:filter(fun isPal/1,cand())).*
*
*
Functions in Erlang are identified by name *and* arity. That's why you need
to explicitly add the arity and use the fun syntax to pass functions around.
*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Lead Developer
On Wed, Aug 28, 2013 at 8:54 AM, Nadav Chernin <nadavchernin@REDACTED>wrote:
> Hi, all
>
> I'm learning Erlang with Project Euler.
>
> This is my solution for problem 4.
>
>
> *-module(euler).*
> *-export([problem4/0]).*
> *
> *
> *cand()->[X*Y||X<-lists:seq(100,999),Y<-lists:seq(100,999)].*
> *isPal(X)->integer_to_list(X)==lists:reverse(integer_to_list(X)).*
> *problem4()->lists:max(lists:filter(fun(A)->isPal(A) end,cand())).*
> *
> *
> Before it, my code was:
>
> *-module(euler).*
> *-export([problem4/0]).*
> *
> *
> *cand()->[X*Y||X<-lists:seq(100,999),Y<-lists:seq(100,999)].*
> *isPal(X)->integer_to_list(X)==lists:reverse(integer_to_list(X)).*
> *problem4()->lists:max(lists:filter(isPal,cand())).*
> *
> *
> Difference from my final solution - using isPal instead fun(A)->isPal(A)
> end
>
> But exception occured during execution:
>
> exception error: no function clause matching
> lists:filter(isPal,
>
> [10000,10100,10200,10300,10400,10500,10600,
>
> 10700,10800,10900,11000,11100,11200,11300,
>
> 11400,11500,11600,11700,11800,11900,12000,
> 12100,12200,12300,12400,12500,12600|...])
>
> Please, explain me why i can't use function as is, without using lambda
> function.
> I come to Erlang from Haskell, and there i can do this,
>
> Thanks, Nadav
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130828/86e4ab4e/attachment.htm>
More information about the erlang-questions
mailing list