[erlang-questions] List comprehension puzzler

Lloyd R. Prentice lloyd@REDACTED
Tue Sep 20 16:31:39 CEST 2016


Wow!

I very much appreciate this improvement.

Lloyd

Sent from my iPad

> On Sep 20, 2016, at 10:17 AM, Pagayon, Ruel <ruel@REDACTED> wrote:
> 
> Hello Lloyd,
> 
> One thing to help optimise your code: Guards
> 
> isbn_format_valid(ISBN) when length(ISBN) == 10 orelse length(ISBN) == 13 ->
>   [I || I <- ISBN, I < $0 orelse I > $9] == [];
> 
> isbn_format_valid(_ISBN) ->
>   false.
>    
> 
> This makes your "is the number equal or between 0 and 9" only be executed if the length is 10 or 13. Reason for this, is if user inputs a very large list, you won't have to compare every character only to find later that it's not the right length (your machine will take the toll).
> 
> Also, you may have noticed we replaced or with orelse, this makes the second expression be evaluated if the ISBN length isn't 10.
> 
> Cheers,
>   
> 
> Ruel Pagayon - ruel@REDACTED
> Platform Engineer
> Networking and Communications
> 
>> On Tue, Sep 20, 2016 at 9:44 PM, <lloyd@REDACTED> wrote:
>> 'Lo again Hernando,
>> 
>> Shortly after I responded to your post I realized that you might be interested in the problem that led to my list comprehension puzzler question:
>> 
>> I'm working on a web application that requires users to enter International Standard Book Numbers (ISBNs). Thus I need a function to validate the format of user input.
>> 
>> ISBNS are either 10 or 13-digits. The standard specifies a check-sum algorithm, but that seemed too complicated for my purposes. So, after struggling with the list comprehension puzzler and Dan Gudmundsson's helpful input, I came up with this:
>> 
>> isbn_format_valid(ISBN) ->
>>    D = fun(I) -> (I >= $0) and (I =< 9) end,
>>    NotIntegers = [I || I <- ISBN, D(I) == true],
>>    Flag1 = NotIntegers == [],
>>    Flag2 = (length(ISBN) == 13) or (length(ISBN) == 10),
>>    Flag1 and Flag2.
>> 
>> As you can see, our minds ran down the same track.
>> 
>> There may be a faster, more elegant, way to solve the problem, but this seems to do the job. Now if I can only figure out how to use the Library of Congress API to retrieve book data...
>> 
>> Thanks again for your post,
>> 
>> Lloyd
>> 
>> 
>> 
>> 
>> -----Original Message-----
>> From: "Lloyd R. Prentice" <lloyd@REDACTED>
>> Sent: Tuesday, September 20, 2016 12:41am
>> To: "Hernando Gisinger" <hgisinger@REDACTED>
>> Cc: "erlang-questions@REDACTED" <erlang-questions@REDACTED>
>> Subject: Re: [erlang-questions] List comprehension puzzler
>> 
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>> Hi Hernando,
>> 
>> I came up with almost the exact same code after Dan Gudmunsson pointed out the error  of my ways.
>> 
>> Many thanks for the solution.
>> 
>> Best wishes,
>> 
>> Lloyd
>> 
>> Sent from my iPad
>> 
>> > On Sep 19, 2016, at 11:01 PM, Hernando Gisinger <hgisinger@REDACTED> wrote:
>> >
>> > Hello
>> >
>> > 1> IsDigit = fun(C) -> C >= $0 andalso C =< $9 end.
>> > #Fun<erl_eval.6.52032458>
>> > 2> S = "123a456".
>> > "123a456"
>> > 3> [IsDigit(I) || I <- S].
>> > [true,true,true,false,true,true,true]
>> >
>> >
>> > 2016-09-18 13:56 GMT-03:00 <lloyd@REDACTED>:
>> >> Hello,
>> >>
>> >> Now this I would not expect:
>> >>
>> >> 4> S = "123a456".
>> >> "123a456"
>> >>
>> >> 5> is_integer(S).
>> >> false
>> >>
>> >> 6> [is_integer(I) || I <- S].
>> >> [true,true,true,true,true,true,true]
>> >>
>> >> Please tell me what I don't understand.
>> >>
>> >> Many thanks,
>> >>
>> >> LRP
>> >>
>> >>
>> >> *********************************************
>> >> My books:
>> >>
>> >> THE GOSPEL OF ASHES
>> >> http://thegospelofashes.com
>> >>
>> >> Strength is not enough. Do they have the courage
>> >> and the cunning? Can they survive long enough to
>> >> save the lives of millions?
>> >>
>> >> FREEIN' PANCHO
>> >> http://freeinpancho.com
>> >>
>> >> A community of misfits help a troubled boy find his way
>> >>
>> >> AYA TAKEO
>> >> http://ayatakeo.com
>> >>
>> >> Star-crossed love, war and power in an alternative
>> >> universe
>> >>
>> >> Available through Amazon or by request from your
>> >> favorite bookstore
>> >>
>> >>
>> >> **********************************************
>> >>
>> >> _______________________________________________
>> >> erlang-questions mailing list
>> >> erlang-questions@REDACTED
>> >> http://erlang.org/mailman/listinfo/erlang-questions
>> >
>> 
>> 
>> _______________________________________________
>> 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/20160920/254cdfba/attachment.htm>


More information about the erlang-questions mailing list