Search code examples
mapreduceraku

Check if all elements of list are prime in Raku


my @g = (1,2,3,4);
say reduce {is-prime}, @g; # ==> gives error
say reduce {is-prime *}, @g; #==> gives error
say reduce {is-prime}, (1,2,3,4); # ==> gives error
say so is-prime @g.all; # ==> gives error

How to check if all elements of list are prime in Raku?


Solution

  • You're basically asking: are there any elements in this list which are not prime? I would write that as:

    say "not all prime" if @g.first: !*.is-prime;
    

    Please note though, that apparently 1 is not considered prime according to the is-prime function:

    say 1.is-prime;  # False
    

    so the first would trigger on the 1 in your example, not on the 4.