[erlang-questions] Question about lists:filter

Steve Strong steve@REDACTED
Wed Aug 28 13:59:06 CEST 2013


In your second sample, isPal is just an atom, hence the error.  Instead, use "fun isPal/1" to indicate that you're passing a function. 

Cheers,

Steve 

-- 
Steve Strong
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


On Wednesday, 28 August 2013 at 13:54, Nadav Chernin 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 (mailto: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/446e113e/attachment.htm>


More information about the erlang-questions mailing list