3 ms·
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).
by joeyh 11y ago
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] ]
- raiph 11y agoFwiw, 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)
- Grim_Reaper 11y agoPython list comprehension similar to Haskell -> https://en.wikipedia.org/wiki/List_comprehension#Python https://en.wikipedia.org/wiki/List_comprehension#Python