Live data from Hacker News

Haskell to Perl 6

docs.perl6.org

71–80 of 136 posts

Re: Haskell to Perl 6

#71
post #49
post #11

There's also a Python version of this[1], which starts with a discussion about Perl 6's equivalent of print, which is put. This makes no sense to me whatsoever, as print is both a much easier way to say it and _what was used in Perl 5_, but not my circus, not my monkeys. But then it goes on to say: > There is also say, which behaves similarly, but will call the gist method of its argument. And after having read the l…

print in Perl 6 works the same as print in Perl 5. put/say are output functions which automatically add a newline at the end. If you want fancier formatting, printf is available as well. And notice the relative lengths of these names. As much as possible, p6 is organized so that the more commonly used thing will have the shorter name. PS I don't think I have ever used put in p6. My code uses say, or print if I don't…

As for the say function, it's also supposed to output text in a more human readable format. It calls the `.gist` of its argument, which you can override in user-defined classes. Take for example the following code snippet from Perl 6 Deep Dive:

    class Chemical { 
        has $.formula; 
        method gist { 
            my $output = $!formula; 
            $output ~~ s:g/()/{(0x2080+$0).chr}/; 
            $output; 
        }
     }


     my $chem = Chemical.new(formula => 'Al6O13Si2'); 
     say $chem; # Al₆O₁₃Si₂

     #`[
     Without overriding the `gist` method:

     say $chem; # Chemical.new(formula => 'Al6O13Si2')
     ]

Re: Haskell to Perl 6

#72
post #37

Earlier quoted context omitted.

Except the resulting code will look nothing like “the humans say it”, and instead will be a cryptic charade requiring a ton of knowledge to decipher - ending up being neither natural language nor machine code.

> Except the resulting code will look nothing like “the humans say it”, Actually, I find it maps rather directly to how a lot of people explain things. For example, "Examine each number, and as long as it's greater than 10 and odd, then pass it to the the work function." for my $number ( @numbers ) { next unless $number > 10; next unless $number % 2; work($number); } Perl generally allows you to structure your code a…

In Perl 6 this could almost look the same:

    for @numbers -> $number {
        next unless $number > 10;
        next unless $number % 2;
        work($number);
    }
Although personally, I wouldn't write it like that: I would probably write it as:

    for @numbers.grep( { $_ > 10 && $_ % 2 } ) -> $number {
        work($number)
    }
or use the postfix notation:

    work($_) for @numbers.grep( { $_ > 10 && $_ % 2 } );
If you want to spread this out over multiple CPUs and you don't care about the order in which the work is done, you only need to add the `.race` method:

    for @numbers.grep( { $_ > 10 && $_ % 2 } ).race -> $number {
        work($number)
    }
More info: https://docs.perl6.org/routine/race

Re: Haskell to Perl 6

#73

I'd like to hear opinions on how the use of sigils and the other syntax oddities like Int:D, Int:U, given, when, etc improves the codabliity/readability of Perl 6 vs other languages. The examples vs Haskell are not helping me understand. Personally I don't think the added verbosity is helping in any way.

If Perl is too verbose for you, you can always go to APL.

I think APL wins hands down in being the most concise when it comes to mathematical tasks, but P6 wins at more common scripting tasks such as parsing data. Only Rebol & Red in my opinion have similar constructs to P6's native grammars. Rebol wins at being the quickest way to write short GUI applications that do useful things as it uses its GUI dialect (basically a dsl) to make it really easy....almost unbelievably so (http://www.re-bol.com/rebol.html). I'd say powershell is the most concise way to deal with Windows automation.

So really...all depending on what you mean.

Re: Haskell to Perl 6

#74
post #9

Earlier quoted context omitted.

Perl has never been about uniformity. Perl is a great experiment in language design, or, rather, a battery of experiments. It's the opposite of a cleanly designed language.

I do agree that Perl (6) is not a cleanly designed language.

Would you care to elaborate on the non-clean parts in your opinion?

Re: Haskell to Perl 6

#75
post #55

Earlier quoted context omitted.

No, it's designed. It's just not designed the way everyone expects a computer language to be designed. It was designed by a linguist. He produced a language with some of the flexibility (and messiness) of human languages, which is completely foreign as a goal to other languages. One of the places this shows up to me is being able to say "it" (in essence). That is, we humans, when talking to each other, we say things…

I'm not sure your definition of 'it' (that is, a default) matches how 'it' is actually used. 'It' is a locally scoped name binding to a previously mentioned symbol. In English, the referent of 'it' is sometimes ambiguous, requiring contextual intelligence that computers lack, so programming languages with 'it' need to be more precise. Here's 'it' in lisp: https://en.wikipedia.org/wiki/Anaphoric_macro

The official name for "it" in Perl 6 is "the topic variable":

https://docs.perl6.org/language/variables#index-entry-topic_...

Re: Haskell to Perl 6

#76
post #21

Earlier quoted context omitted.

Except the resulting code will look nothing like “the humans say it”, and instead will be a cryptic charade requiring a ton of knowledge to decipher - ending up being neither natural language nor machine code.

Why would Perl repeat the same mistake as others, of trying to treat a programming language too much like a natural language? It's optimized to be read as a programming language, not anything else. Also, "cryptic charade" could be a good name for what all non-Perl 6 languages call "regular expressions". It would make an even better name for a prog rock band.

Perl (5 or 6) doesn't try to treat a programming language as a natural language. Instead it tries to allow the structure in which the author's brain defines the problem to be able to match the structure in which the code of the program defines the solution. It is linguistic in the sense that it tries to follow the normal flow of human language as a form of communication, allowing skills learned for human communication to be re-used for communicating via source code--not in a natural, human language (still in a computer language) but without necessarily having to pivot the problem in to something more natural for computers first.

Source code is instructions for two audiences: A human, and a computer. The computer doesn't care what the syntax is, but the human does. It is an ongoing challenge to write code in a manner which effectively and concisely communicates to other humans, including your future self. This is a thing at which Perl5 and Perl6 both excel, Perl6 more so IMO (although there is not yet enough history to prove it).

It is true that one can easily write unmaintainable spaghetti in Perl5. One can write unmaintainable spaghetti in any language, but Perl5 makes it somewhat easier to do it by mistake--this is an aspect of the same versatility which gives it its expressive power, which in turn is what makes it possible to communicate things using Perl more effectively (in the same space) than with many other computer languages. You have to employ some discipline not to sound like a gibbering lunatic. This is not much different from human languages: if you have ever read English 101 papers you will find some of them sadly similar to Perl5 scripts written by similarly undisciplined authors.

In contrast to Perl5, Perl6 makes it far easier to avoid accidental spaghetti and does so without sacrificing any of the linguistic expressiveness that makes Perl generally so useful.

Re: Haskell to Perl 6

#77
post #69
post #14

Earlier quoted context omitted.

So, I sort of took that away from it, but there's stuff in the gist page like: > The default gist method in Mu re-dispatches to the perl method for defined invocants, and returns the type name in parenthesis for type object invocants. Many built-in classes override the case of instances to something more specific that may truncate output. And I have no idea how anyone that isn't already fluent in Perl 6 is expected t…

You cannot blame the language reference manual for exposing all the complexity and using precise wording. You are not expected to read that. Other resources exist which are targeted at newcomers and explain things differently.

The reference manual is what was linked to to explain terms that a resource aimed at people who know Python but not Perl 6 used.

Re: Haskell to Perl 6

#78
post #14

Earlier quoted context omitted.

I've never used Perl6, but it took me about 30 seconds to figure out what 'say' and 'gist' do from the linked docs? 'gist' prints a string representation of an object aimed at being human-readable at the expense of not necessarily being a complete representation (e.g. the doc page gives an example that an array has only its first 100 elements printed followed by a '...'). 'say' is a version of 'put' that does this fo…

So, I sort of took that away from it, but there's stuff in the gist page like: > The default gist method in Mu re-dispatches to the perl method for defined invocants, and returns the type name in parenthesis for type object invocants. Many built-in classes override the case of instances to something more specific that may truncate output. And I have no idea how anyone that isn't already fluent in Perl 6 is expected t…

If I went on to learn about "Functors, Applicative Functors and Monoids" (which I'm completely ignorant about) in Haskell without having first learned Haskell's basic building blocks, then I would be disingenuous myself if I blamed the language for my ignorance and unfamiliarity with the language. On top of that, if you're not a Haskell user I don't think you're the target audience as pointed out by bmn__. For this reason, there are several migration guides (Python, Ruby, etc.) which ,although incomplete, they give the languages' respective users a contrast against Perl 6 which hopefully translates into some familiarity with it.

If you want to start from the bottom out, there's the ever improving language docs: https://docs.perl6.org/language.html

Re: Haskell to Perl 6

#79
post #26
post #14

Earlier quoted context omitted.

So, I sort of took that away from it, but there's stuff in the gist page like: > The default gist method in Mu re-dispatches to the perl method for defined invocants, and returns the type name in parenthesis for type object invocants. Many built-in classes override the case of instances to something more specific that may truncate output. And I have no idea how anyone that isn't already fluent in Perl 6 is expected t…

I have no problem reading that. Sure, I dont understand it in fine technical detail (what's Mu? An implementation of Perl? A codename for Perl 6?) But I believe I understand enough of it to start using it. This specific example seems similar to the __repr__ vs __str__ thing in Python, and not very alien at all.

As pointed out by others, Mu is the most undefined value there is in Perl 6 and the base root for the immediate child classes (https://docs.perl6.org/images/type-graph-Mu.svg). However, most classes (both built-in and user-defined ones) don't inherit directly from it. Instead, they inherit from Any, which in turns inherits from Mu.

More info about it:

- https://docs.perl6.org/type/Mu

- https://en.wikipedia.org/wiki/Mu_%28negative%29

Re: Haskell to Perl 6

#80
post #37

Earlier quoted context omitted.

Except the resulting code will look nothing like “the humans say it”, and instead will be a cryptic charade requiring a ton of knowledge to decipher - ending up being neither natural language nor machine code.

> Except the resulting code will look nothing like “the humans say it”, Actually, I find it maps rather directly to how a lot of people explain things. For example, "Examine each number, and as long as it's greater than 10 and odd, then pass it to the the work function." for my $number ( @numbers ) { next unless $number > 10; next unless $number % 2; work($number); } Perl generally allows you to structure your code a…

This would be one way to write in Perl 6:

    for @numbers -> $number {
        next unless $number > 10;
        next if $number %% 2;
        work($number);
    }
Post reply on HN