[erlang-questions] Question about lists:filter

Ludovic Demblans ludovic@REDACTED
Thu Aug 29 00:29:39 CEST 2013


lists:filter/2 want a fun for it first argument. isPal is not a fun but an  
atom.

You must use lists:filter(fun isPal/1, [...]).

Remember, in Erlang, as several functions can share the same name (with  
different arities), a complete function name goes with arity. so : fun  
functionName/Arity

Cheers


Le Wed, 28 Aug 2013 13:54:24 +0200, Nadav Chernin <nadavchernin@REDACTED>  
a écrit:

> 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


-- 
Utilisant le logiciel de courrier révolutionnaire d'Opera :  
http://www.opera.com/mail/



More information about the erlang-questions mailing list