Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Generate the perl sequence 1, 11, 111, ....

by Fletch (Chancellor)
on Oct 10, 2008 at 13:14 UTC ( #716446=note: print w/ replies, xml ) Need Help??


in reply to Generate the perl sequence 1, 11, 111, ....

ones = "1" : [ x ++ "1" | x <- ones ]

That and Language::Haskell should get you all the 1s you need . . .

The cake is a lie.
The cake is a lie.
The cake is a lie.


Comment on Re: Generate the perl sequence 1, 11, 111, ....
Download Code
Re^2: Generate the perl sequence 1, 11, 111, ....
by TimToady (Parson) on Oct 10, 2008 at 17:41 UTC
    Tsk, tsk, Haskell is such a verbose language. :) In Perl 6 it' just:
    [\~] 1 xx *
    where that just says "make an arbitrarily long list of 1's and reduce them using concatenation (the ~ operator), returning partial results in a list". That even works today in pugs if you replace the * with, say, 10:
    pugs> .say for [\~] 1 xx 10 1 11 111 1111 11111 111111 1111111 11111111 111111111 1111111111
    Or using the new, spiffily readable ... infix, it's even shorter:
    1...{$_~1}
    That just extends the list on the left by concatenating a 1 onto the previous last value of the list. But nobody has implemented that one yet, since we only invented it on Sunday... :)

    The cool thing about it is that (if you're not golfing it) you can write however much of the list you like as an example for the human reader, and give the parameter a better name:

    1,11,111,1111 ... {$^previous ~ 1}

    Update: nowadays (2010/04/03) the closure goes on the left, so we'd write those more like:

    1,*~1...* 1, 11, 111, 1111, {$^previous ~ 1} ... *
        Yes, we really will. We have a complete parser now, written in Perl 6, that parses itself, and can spit out an AST. All that remains is to come up with a VM with sufficent support for lazy functional OO semantics, and write an emitter for it. Simple! :)
        I know we're not allowed to ask when, so I'll ask if we really will? Xmas of 2010? ;)

        I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
      Disclaimer: although the series operator is fairly new, it was introduced before TimToady read this question ;-)
      where that just says "make an arbitrarily long list of 1's and reduce them using concatenation (the ~ operator), returning partial results in a list".

      I personally believe that I need some clarifications: I understand clearly that the backslash implements the "returning partial results" thingie, but I am not sure about is semantics. In particular, is it a metaoperator modifying any binary operator, and if so, how? Or is really [\ ] a distinct metaoperator wrt [ ], with the former being the "reduction returning partial results" and the latter simply "reduction?"

      --
      If you can't understand the incipit, then please check the IPB Campaign.
        Use the source, Luke.
        regex prefix_circumfix_meta_operator:reduce (--> List_prefix) { $<s> = ( '[' [ | <op=infix> ']' [''|<?>] | <op=infix_prefix_meta_operator> ']' [''|<?>] | <op=infix_circumfix_meta_operator> ']' [''|<?>] | \\<op=infix> ']' [''|<?>] | \\<op=infix_prefix_meta_operator> ']' [''|<?>] | \\<op=infix_circumfix_meta_operator> ']' [''|<?>] ] ) <?before \s | '(' > ...

        So [\ is really a handled specially.

      I have one more consideration, and a pair of questions, which I forgot to mention when I wrote my previous followup:

      1. I don't know if it's the same reason why that particular symbol was chosen, but I find the appearance of [\ ] to be extremely representative of the "take partial results" thingie since it looks much like a triangle which is maximally evident here, given that the output "is also a triangle" but has more general sense with common reduction situations one may naturally feel psychologically at ease with, charachterized by "results that grow." (Sorry for the sloppy parliance, which I believe to be appropriate here, though.)
      2. I also wonder if the same behaviour can be obtained by means of a suitable adverb (i.e. whether one such adverb is provided!) acting on [ ]... well if adverbs do apply to meta-operators at all, that is, which I ignore altogher;
      3. last and perhaps most importantly, given that [\ ] returns the list of partial reductions, shouldn't it for completeness also include the 0-th one? Namely, the neutral element for the bynary operation passed to it, if any. (I already can feel that the "if any" bit could prompt for a negative answer, yet I feel that a positive one would be mathematically more appropriate.)
      --
      If you can't understand the incipit, then please check the IPB Campaign.
        1. I don't know if it's the same reason why that particular symbol was chosen, but I find the appearance of [\ ] to be extremely representative of the "take partial results" thingie since it looks much like a triangle which is maximally evident here, given that the output "is also a triangle" but has more general sense with common reduction situations one may naturally feel psychologically at ease with, charachterized by "results that grow." (Sorry for the sloppy parliance, which I believe to be appropriate here, though.)
        Indeed, language design is "sloppy", which is why almost nothing goes into the design unless there are multiple good reasons for it. But yes, the triangular visual metaphor was certainly one of the reasons for picking the backslash. Another reason is simply that very few infix operators (read "zero" in standard Perl 6) begin with backslash. Another visual or psychological consideration is that a more complicated result should be represented by a "larger" operator. A very minor consideration is that backslash tends to "escape" things into their "meta" equivalent, though what exactly is being "escaped" here is not at all clear. As for the use of square brackets on both this and the unbackslashed forms, that is justified by the cultural association with list constructors and subscripts that come from the earliest days of Perl (not to mention the fact that many functional programming languages denote lists using square brackets). And in the backslashed form, it needs something with a vertical feel on the left representing the "left margin", or we lose much of the triangular feeling, so square brackets work nicely together with backslash that way. It all adds up to an almost overwhelming feeling of necessity--which is why the language designer must also guard against adding up too many apples and then comparing them to orangutans.
        2. I also wonder if the same behaviour can be obtained by means of a suitable adverb (i.e. whether one such adverb is provided!) acting on [ ]... well if adverbs do apply to meta-operators at all, that is, which I ignore altogher;
        I personally believe that adverbs are more usefully applied to the base operator, and currently such adverbs must really go outside the metaoperator since metaoperators are designed to be parsed as tokens, which don't like spaces or other complicated syntax inside, or it becomes impossible to follow the longest token rule. If we consider [min] to be a single token, then what will the lexer make of something like: [min :cmp{ $^a leg $^b }]? So currently I consider metoperators to be transparent to adverbs. (And again, there are other considerations--multiplicative factors on the number of meta-modified lexemes tends to result in an explosion of possibilities for the DFA matcher that is feebly trying to figure out the next token without using up gigabytes of stack and heap.)
        3. last and perhaps most importantly, given that [\ ] returns the list of partial reductions, shouldn't it for completeness also include the 0-th one? Namely, the neutral element for the bynary operation passed to it, if any. (I already can feel that the "if any" bit could prompt for a negative answer, yet I feel that a positive one would be mathematically more appropriate.)
        Funny thing, I was just thinking about that myself. As usual, I can argue it all three ways:
        • Completeness demands that we return the 0th element, and consistency demands that we do so even for operations that have no neutral element, which should therefore return undef for the 0th result. The user can always throw it away.
        • The whole point of definedness is to tell you when something is defined, so rely on that to decide whether to return the neutral element. Nobody is going to go around comparing two such lists to see if they're the same, since they're potentially infinite, and even if they did, they'd want to normalize out any initial undefined element on the assumption that different ways of specifying the same sequence might or might not produce such an element.
        • Look, you two idiots, the current behavior is the sanest, insofar as, if the user really wants the stupid neutral element to come out first, all they have to do is supply it as the first argument!
        So maybe we should decide this in the next PerlMonks poll. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716446]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-12-18 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls