[erlang-questions] List comprehension puzzler

Fred Hebert mononcqc@REDACTED
Wed Sep 21 15:03:37 CEST 2016


On 09/21, ok@REDACTED wrote:
>It's not much harder to write
>
>length_between([_|Xs], L, U) when L > 0 ->
>    length_between(Xs, L-1, U-1);
>length_between([], L, U) ->
>    L =< 0, 0 =< U;
>length_between(_, _, _) ->
>    false.
>
>and use it the same way.  And of course, while it's
>*nice* to do pattern matching in the head, we *do* have 'case'.
>
Your Prolog is showing! The second clause there should have the body:

    length_between([], L, U) ->
        L =< 0 andalso 0 =< U;

since the comma (`,') will execute the comparison in sequence, but only 
the result of `0 =< U' will be returned.



More information about the erlang-questions mailing list