6 ms·
# Lazy list of Fibonacci numbers up to (but excluding) 100: my @fib := 1, 2, *+* ...^ * >= 100;
by bokchoi 16y ago
# Lazy list of Fibonacci numbers up to (but excluding) 100:
my @fib := 1, 2, *+* ...^ * >= 100;
- deleted 16y ago[deleted]
- bkudria 16y agoPerl, proudly wearing the "Looks Like Line Noise" hat into 2011!
- patrickas 16y agoI think a more apt retort is "looks like Chinese" because when/if you learn Perl6 I assure you that line will seem natural, readable and meaningful. Still you are perfectly free to not learn Perl6 nor Chinese :-)
- bkudria 16y agoAre you saying English and Chinese are both exactly as easy to learn, write, and read?
- berntb 16y agoOh, please... If you don't know short notations in e.g. math, it will look like line noise. The analogy is halting because he used natural languages -- he probably didn't want to insult you by explaining curried functions and CS... :-) And yes, I needed masak's explanation, too. The Pascal ":=" brings back happy memories of childhood...
- patrickas 16y agoNo I am not saying that. Sorry for not being clearer. I am pretty sure they are not exactly as easy, but I have no idea which is easier! That's an interesting question though; do you know of any studies on that subject?
- viraptor 16y agoBut at what cost? Of course if you spend X hours on idea Y, it will seem more natural, readable and meaningful (if you spend too much time, it might seem meaningful even if it's wrong...). Here, you get a mash of so many concepts in one line that it's hard to claim any readability really... From the start, you get list (@), one of 3 possible assignments (:=), separators and operators at different levels (, , ...^, (star), >=), math symbols in two different contexts (+, (star)), meaningful position in a list (seed, seed, function). On top of that, to read it without parens you have to keep all operator priorities in mind. Readable? I have a hard time finding one (apart from J, K, APL, etc. family and silly ones) that is less readable than this sample. The "looks like Chinese" comparison is really apt - Chinese takes considerably more effort to learn than many other languages.
- masak 16y agoI could manipulate physical nature if I only learn mathematical notation, but at what cost? Here's the thing: I like Perl 6 because of the abstractions it gives me. In fact, I like learning new programming languages because of what I learn about ways of slicing the solution space. It's like learning new spoken languages, only easier. I spend the time I could spend complaining about some unknown language's syntax learning that language, trying to enrich my repertoire as a programmer.
- viraptor 16y agoI agree in general, as I try to learn at least one new language a year and it's a great exercise. However, I think some of them are crossing the line (perl5, 6 even more) and honestly believe that I would save at least a couple of weeks of my time at work this year if I wouldn't have to chase Perl code bugs that would be completely obvious in other languages. Both due to the crazy syntax and due to not exploding with an exception the moment something looks silly (warnings, strict, autodie and others are pretty much mandatory in code I get for review). At this point I'm an anti-perl advocate - and again, I believe it had positive effects at work. It goes beyond complaining about "unknown syntax".
- 16y ago
- btucker 16y agoWould someone be willing to break this down and explain it?
- masak 16y agoBy all means. my @fib := ...; Bind something to an array variable. Assignment tends to follow infinite lists into the abyss. (But would also work, in this case, since the list is lazily evaluated.) 1, 2, *+* ...^ * >= 100; This is all governed by the `...^` in the middle, which is a lazy list generator. Three components, the seed, the iteration and the end condition, surround it. (Those three are much like a C `for` loop, come to think of it.) The seed is simply `1, 2`. The iteration is star-plus-star (which I can't figure out how to write out explicitly in this markup). This is ultimately syntactic sugar for a lambda with two parameters: `-> $a, $b { $a + $b }`. Or, put differently, this is a curried addition function. The end condition is `* >= 100`. Also curried; we could desugar it as either `-> $c { $c >= 100 }` or simply `{ $_ >= 100}`. It's a block that takes one argument and checks whether it's 100 or greater. The sequence operator `...^` has a little hat on it, meaning we're to discard the item that eventually matches the end condition. This is congruent with the fact that there is a range operator `..^` (note: two dots) which excludes its right endpoint.
- mjs 16y agoThanks for breaking that down, it's all, uh, a little terse. The ..^ operator (including the right endpoint?) doesn't do what I was expecting: > 1, 2, * + * ..^ * >= 100; 1 2 _block298 Also, this seems a little unfortunate: > 1, * + * ...^ * >= 100; ===SORRY!=== Method 'Bridge' not found for invocant of class 'Failure' (I was expecting some warning about the closure expecting two arguments, and only getting one.)
- masak 16y agoFrankly, so was I. I'll get back to you on that.
- jparise 16y agoHere's a (rough) Python equivalent: from itertools import islice def fib(): a, b = 0, 1 while True: yield a a, b = b, a + b for k in islice(fib(), 1, 100): print(k) It uses a generator function to produce an infinite iterable sequence. islice() is used to produce just a slice of that sequence. This could probably be written using fewer characters, but I don't think it could be anywhere near a short (and terse) as the Perl version.
- masak 16y agoCorrect me if I'm wrong, but that code prints 100 fibonacci numbers, whereas the Perl 6 code prints all fibonacci numbers up to, but not including, 100.
- jparise 16y agoYou're right. I misread the range of the Perl version. Adjust the slice accordingly.
- masak 16y agoNo, that's too easy a cop-out. :-) How would you stop at the right number in Python code? Would you compute 100 numbers and then inspect/truncate afterwards?
- jparise 16y agoAh, I see that I truly missed your earlier point. I suppose I would just do this: def fib(max): a, b = 0, 1 while a < max: yield a a, b = b, a + b for k in fib(100): print(k)
- joeyh 16y agoAnd a haskell version (not the shortest possible one): takeWhile (< 100) fibs where fibs = 0 : 1 : zipWith (+) fibs $ tail fibs I don't think fibs can be expressed as a list comprehension in haskell, can it? It's cute that perl6 is able to use one, but OTOH I can't think of many other lists I'd want to use that trick to calculate. And, despite having written perl for 15 years, I find the haskell version much clearer, and its use of laziness and infinite lists (zipping them up in particular) much more advanced than perl6's.
- perlgeek 16y agoreddit and hackernews are really similar: both pick a single line or two out of a >1k words post and ignore the rest. Impressive.