<div dir="ltr">While in C... length(ISBN) would still be O(n) and possibly unbounded. I don't know if there is any optimization in the guard to cause the second length() to be skipped but that still seems expensive. <br><br>Probably best to keep count while you check or maybe something ugly like <br><br>isbn_format_valid([A,B,C,D,E,F,G,H,I,J|Tail]) -><div> check_A_to_J andalso isbn_sub_format_valid(Tail).<br></div><div>isbn_format_valid(_) -></div><div> false.</div><div><br></div><div>isbn_sub_format_valid([]) -></div><div> true;</div><div>isbn_sub_format_valid([A,B,C]) -></div><div> check_A_to_C.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 20, 2016 at 10:17 AM, Pagayon, Ruel <span dir="ltr"><<a href="mailto:ruel@ruel.me" target="_blank">ruel@ruel.me</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello Lloyd,<div><br></div><div>One thing to help optimise your code: Guards</div><div><br></div><div><span style="font-size:12.8px"><font face="monospace, monospace">isbn_format_valid(ISBN) when length(ISBN) == 10 orelse length(ISBN) == 13 -></font></span></div><div><font face="monospace, monospace"><span style="font-size:12.8px"> </span><span style="font-size:12.8px">[I || I <- ISBN, </span><span style="font-size:12.8px">I < $0 orelse I > $9</span><span style="font-size:12.8px">] == [];</span></font></div><div><span style="font-size:12.8px"><font face="monospace, monospace"><br></font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace">isbn_format_valid(_ISBN) -></font></span></div><div><span style="font-size:12.8px"><font face="monospace, monospace"> false.</font></span></div><div><span style="font-size:12.8px"> </span></div><div><br></div><div><span style="font-size:12.8px">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).</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Also, you may have noticed we replaced <b><font face="monospace, monospace">or</font></b> with <b><font face="monospace, monospace">orelse</font></b>, this makes the second expression be evaluated if the ISBN length isn't 10.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Cheers,</span></div><div><span style="font-size:12.8px"> </span></div></div><div class="gmail_extra"><br clear="all"><div><div data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><p style="font-family:arial,sans-serif;font-size:medium;margin-bottom:0.0001pt;line-height:normal"><b><span style="font-size:7.5pt;color:#236b8e;background:white">Ruel
Pagayon</span></b><span style="font-size:7.5pt;color:#236b8e;background:white"> - <a href="mailto:ruel@ruel.me" target="_blank"><span style="color:#0000cc">ruel@ruel.me</span></a></span><span style="font-size:10.0pt;color:#888888;background:white"><br></span><span style="font-size:7.5pt;font-family:Arial,sans-serif;background-color:white;background-repeat:initial initial"><font color="#666666">Platform Engineer<br></font></span><span style="color:rgb(153,153,153);font-family:Arial,sans-serif;font-size:10px;background-color:rgb(255,255,255)">Networking and Communications</span></p></div></div></div></div></div></div></div></div><div><div class="h5">
<br><div class="gmail_quote">On Tue, Sep 20, 2016 at 9:44 PM, <span dir="ltr"><<a href="mailto:lloyd@writersglen.com" target="_blank">lloyd@writersglen.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">'Lo again Hernando,<br>
<br>
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:<br>
<br>
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.<br>
<br>
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:<br>
<br>
isbn_format_valid(ISBN) -><br>
D = fun(I) -> (I >= $0) and (I =< 9) end,<br>
NotIntegers = [I || I <- ISBN, D(I) == true],<br>
Flag1 = NotIntegers == [],<br>
Flag2 = (length(ISBN) == 13) or (length(ISBN) == 10),<br>
Flag1 and Flag2.<br>
<br>
As you can see, our minds ran down the same track.<br>
<br>
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...<br>
<br>
Thanks again for your post,<br>
<br>
Lloyd<br>
<div><div><br>
<br>
<br>
<br>
-----Original Message-----<br>
From: "Lloyd R. Prentice" <<a href="mailto:lloyd@writersglen.com" target="_blank">lloyd@writersglen.com</a>><br>
Sent: Tuesday, September 20, 2016 12:41am<br>
To: "Hernando Gisinger" <<a href="mailto:hgisinger@gmail.com" target="_blank">hgisinger@gmail.com</a>><br>
Cc: "<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>" <<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>><br>
Subject: Re: [erlang-questions] List comprehension puzzler<br>
<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
Hi Hernando,<br>
<br>
I came up with almost the exact same code after Dan Gudmunsson pointed out the error of my ways.<br>
<br>
Many thanks for the solution.<br>
<br>
Best wishes,<br>
<br>
Lloyd<br>
<br>
Sent from my iPad<br>
<br>
> On Sep 19, 2016, at 11:01 PM, Hernando Gisinger <<a href="mailto:hgisinger@gmail.com" target="_blank">hgisinger@gmail.com</a>> wrote:<br>
><br>
> Hello<br>
><br>
> 1> IsDigit = fun(C) -> C >= $0 andalso C =< $9 end.<br>
> #Fun<erl_eval.6.52032458><br>
> 2> S = "123a456".<br>
> "123a456"<br>
> 3> [IsDigit(I) || I <- S].<br>
> [true,true,true,false,true,tru<wbr>e,true]<br>
><br>
><br>
> 2016-09-18 13:56 GMT-03:00 <<a href="mailto:lloyd@writersglen.com" target="_blank">lloyd@writersglen.com</a>>:<br>
>> Hello,<br>
>><br>
>> Now this I would not expect:<br>
>><br>
>> 4> S = "123a456".<br>
>> "123a456"<br>
>><br>
>> 5> is_integer(S).<br>
>> false<br>
>><br>
>> 6> [is_integer(I) || I <- S].<br>
>> [true,true,true,true,true,true<wbr>,true]<br>
>><br>
>> Please tell me what I don't understand.<br>
>><br>
>> Many thanks,<br>
>><br>
>> LRP<br>
>><br>
>><br>
>> ******************************<wbr>***************<br>
>> My books:<br>
>><br>
>> THE GOSPEL OF ASHES<br>
>> <a href="http://thegospelofashes.com" rel="noreferrer" target="_blank">http://thegospelofashes.com</a><br>
>><br>
>> Strength is not enough. Do they have the courage<br>
>> and the cunning? Can they survive long enough to<br>
>> save the lives of millions?<br>
>><br>
>> FREEIN' PANCHO<br>
>> <a href="http://freeinpancho.com" rel="noreferrer" target="_blank">http://freeinpancho.com</a><br>
>><br>
>> A community of misfits help a troubled boy find his way<br>
>><br>
>> AYA TAKEO<br>
>> <a href="http://ayatakeo.com" rel="noreferrer" target="_blank">http://ayatakeo.com</a><br>
>><br>
>> Star-crossed love, war and power in an alternative<br>
>> universe<br>
>><br>
>> Available through Amazon or by request from your<br>
>> favorite bookstore<br>
>><br>
>><br>
>> ******************************<wbr>****************<br>
>><br>
>> ______________________________<wbr>_________________<br>
>> erlang-questions mailing list<br>
>> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
>> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
><br>
<br>
<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</div></div></blockquote></div><br></div></div></div>
<br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>