Bill Allombert on Sun, 10 Mar 2013 18:59:33 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: PARI 2.6 syntax 2: [f(x)|x<-a,b(x)]


On Fri, Mar 08, 2013 at 12:41:44AM +0100, Bill Allombert wrote:
> Dear PARI developers,
> 
> In PARI 2.6 I introduced the following syntax:
> [f(x)|x<-V] and
> [f(x)|x<-V,f(x)] 
> 
> for example [x^2|x<-[1..100],isprime(x)]
> returns the list of the square of the prime number less than 100.
> 
> The symbol <- was chosen because it was similar to Haskell syntax, and
> looked similar to both 'element of' â and to a left arrow â, so it 
> accomodates both mathematicians and computer scientists faith.
> 
> Unfortunately the choice of the symbol <- is problematic because
> this leads to an ambiguity between x (<-) a and x < (-a), so we can
> only use it immediatly after a vertical bar |, which prevents generalizing
> the syntax.

An alternative is to consider the operator to be |...<- where ... is a variable
name.

I have added a branch bill-multi_compr which allow multiple use of |...<-:
For example:

? [a^2+b^2|a<-[1..10]|b<-[1..10],gcd(a,b)==1]
%2 = [2,5,10,17,26,37,50,65,82,101,5,13,29,53,85,10,13,25,34,58,73,109,17,25,41,65,97,26,29,34,41,61,74,89,106,37,61,85,50,53,58,65,74,85,113,130,149,65,73,89,113,145,82,85,97,106,130,145,181,101,109,149,181]

? [a^2+b^2|a<-[1..10],isprime(a)|b<-[1..10],a!=b && isprime(b)]
%4 = [13,29,53,13,34,58,29,34,74,53,58,74]

? [[a,b,c]|a<-[1..8]|b<-[1..a]|c<-[1..b]]
%6 = [[1,1,1],[2,1,1],[2,2,1],[2,2,2],[3,1,1],[3,2,1],[3,2,2],[3,3,1],[3,3,2],[3,3,3],[4,1,1],[4,2,1],[4,2,2],[4,3,1],[4,3,2],[4,3,3],[4,4,1],[4,4,2],[4,4,3],[4,4,4],[5,1,1],[5,2,1],[5,2,2],[5,3,1],[5,3,2],[5,3,3],[5,4,1],[5,4,2],[5,4,3],[5,4,4],[5,5,1],[5,5,2],[5,5,3],[5,5,4],[5,5,5],[6,1,1],[6,2,1],[6,2,2],[6,3,1],[6,3,2],[6,3,3],[6,4,1],[6,4,2],[6,4,3],[6,4,4],[6,5,1],[6,5,2],[6,5,3],[6,5,4],[6,5,5],[6,6,1],[6,6,2],[6,6,3],[6,6,4],[6,6,5],[6,6,6],[7,1,1],[7,2,1],[7,2,2],[7,3,1],[7,3,2],[7,3,3],[7,4,1],[7,4,2],[7,4,3],[7,4,4],[7,5,1],[7,5,2],[7,5,3],[7,5,4],[7,5,5],[7,6,1],[7,6,2],[7,6,3],[7,6,4],[7,6,5],[7,6,6],[7,7,1],[7,7,2],[7,7,3],[7,7,4],[7,7,5],[7,7,6],[7,7,7],[8,1,1],[8,2,1],[8,2,2],[8,3,1],[8,3,2],[8,3,3],[8,4,1],[8,4,2],[8,4,3],[8,4,4],[8,5,1],[8,5,2],[8,5,3],[8,5,4],[8,5,5],[8,6,1],[8,6,2],[8,6,3],[8,6,4],[8,6,5],[8,6,6],[8,7,1],[8,7,2],[8,7,3],[8,7,4],[8,7,5],[8,7,6],[8,7,7],[8,8,1],[8,8,2],[8,8,3],[8,8,4],[8,8,5],[8,8,6],[8,8,7],[8,8,8]]

Cheers,
Bill.