[erlang-questions] List comprehension puzzler
Thu Sep 22 10:58:26 CEST 2016
If you ever come back to this problem, let me suggest is_longer/2 as a little more readable alternative. And to make it even more useful let it accept iolists.
On 21 September 2016 16:30:33 GMT+10:00, Raimo Niskanen <> wrote:
>On Tue, Sep 20, 2016 at 03:53:55PM -0700, Kenneth Lakin wrote:
>> On 09/20/2016 08:00 AM, Raimo Niskanen wrote:
>> > Note that length(ISBN) will be called twice on this maybe very long
>> > and that in itself may be bad since length/1 is O(N).
>> That seems easy enough to fix:
>> when length(I) =< 13 andalso (length(I) == 10 orelse length(I) == 13)
>Sorry about being unclear. I should have written "... *called* (twice)
>since there is little greater harm in calling it twice than calling it
>It is still O(N) if you call it a constant number of times, and the
>subsequent times it is likely that data is in the processor's cache
>so they may be faster.
>The harm is in calling length/1 even once on a possibly very long list.
>We have had a suggestion to add a guard length(List, Max)
>or max_length(List, Max) that would stop traversal after Max elements.
>This would be O(min(length(List), Max)),
>which is O(Max),
>which for a constant Max is O(1).
>But we never got to it...
>/ Raimo Niskanen, Erlang/OTP, Ericsson AB
>erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions