Hacker News new | past | comments | ask | show | jobs | submit login

Linear and geometric sequences are auto-detected, for anything else you'll need to provide a function to compute the next element:

  2, 4 ... *        # linear
  2, 4, 8 ... *     # exponential
  1, 1 ... *        # constant
  1, 1, * + * ... * # Fibonacci
The last expression is equivalent to the more verbose variant

  1, 1, -> $a, $b { $a + $b } ... *
that makes use of an 'pointy block' (ie lambda function) instead of the Whatever-*.



Ah so it's similar to the haskell list comprehension syntax, converted to a point-free form (and with added guessing/defaulting due to the ambiguities therein).

    [x*2 | x <- [1..10]] -- linear
    [2^x | x <- [1..10]] -- exponential
    [1 | x <- [1..10]]   -- constant
IIRC haskell list comprehensions can't do fib without naming the list so it can refer to itself. OTOH, these perl list comprehensions seems unable to express things that combine values from several lists, eg:

    [ x*y | x <- [2,4..8], y <- [-1,0,1] ] -- [-2,0,2,-4,0,4,-6,0,6,-8,0,8]
Course, haskell also has a weak form of [x1,x2..xn] syntax for a linear sequence, as in [2,4..8]. That's always felt a bit of an unnecessary wart in haskell to me, although the simpler [x..y] and [x..] are very handy syntaxes.

TBH, I almost never use list comprehensions either, preferring regular functions, and the list monad or applicative.

    (*2) [1..10] -- linear
    (2^) [1..10] -- exponential
    repeat 1     -- constant
    fibs = 0 : 1 : zipWith (+) fibs (tail fibs) -- fibonacci obvs
    (*) <$> [2,4..8] <*> [-1,0,1] -- same as [ x*y | x <- [2,4..8], y <- [-1,0,1] ]


Fwiw, the Haskell:

  [ x*y | x <- [2,4..8], y <- [-1,0,1] ]
  -- [-2,0,2,-4,0,4,-6, 0,6,-8,0,8]
could be translated to the Perl 6:

  [2,4...8] X* [-1,0,1]
  # (-2 0 2 -4 0 4 -6 0 6 -8 0 8)


Alternate pointfree Haskell syntax:

    (*) <$> [2,4..8] <*> [-1, 0, 1]
or

    liftA2 (*) [2, 4..8] [-1, 0, 1]


Sure, but that perl is not using the ... list comprehension syntax to combine the lists.


This is so very Perl. I guess this is Perls answer to list comprehensions.


That's interesting, I always figured Python's list comprehensions where their answer to Perl's map and grep.

But in direct answer to your question, no, but it's part of the answer[1].

1: https://en.wikipedia.org/wiki/List_comprehension#Perl_6


I would assume that it has those too. Due to the main implementation being Pugs for a while, the language absorbed a lot of Haskellisms (chiefly laziness).


gather/take is more the general Perl 6 take on list comprehensions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: