Live data from Hacker News

Haskell to Perl 6

docs.perl6.org

121–130 of 136 posts

Re: Haskell to Perl 6

#121
post #119
post #115

Earlier quoted context omitted.

I'm not sure if I've nailed what the GP was speaking of or what you're looking for but anyway I do think the following is illustrative of the linguistic evolution P6 represents. Earlier this year at perlmonks (perhaps the top resource along with stackoverflow for experienced perl folk answering questions) a poster wanted solutions to a fairly basic operation:[1] > I need a function which will filter a nested hash, re…

Thanks for the reply. Definitely looks like P6 can work at a higher level ...

You're welcome.

Here's another quick example. It may or may not touch on your or the GGGP's point. So, no need to reply. I just thought I'd post something more while we wait for their reply.

    say now - INIT now
This displays the time difference between the normal run-time moment that the `now` call before the minus is called and the `now` call after the minus sign which is run during the earlier INIT phase of execution: https://docs.perl6.org/language/phasers#phasers__INIT

In P5, phased code didn't return values so one needed to create a variable and initialize it elsewhere in the code.

P6 has many more phasers than P5 and allows many of them to return values to code that's run at a different phase. (This is sometimes called "time traveling" code.) This saves a lot of jumping back and forth needed to understand a fragment of code.

Re: Haskell to Perl 6

#122
post #22

The lack of maybe or nullable types in perl6 with the need to check for definedness at run-time (Int:D) is one of its major design blunders. Also this syntax! In cperl I rather use proper maybe types as in every other language. ?Int is (Int | Undef) you cannot use Int? as this would confuse the parser to look for the matching :

> The lack of maybe or nullable types in perl6

P6 supports something more powerful and uses that instead:

https://en.wikipedia.org/wiki/Tagged_union

In P6 the symbol Int, when treated as a type by the compiler, denotes Int:_, which is a sum type, with two subtypes, namely a generically existentially quantified Int (Int:D) and a generically universally quantified Int (Int:U).

Thus Int can be used in the role of a nullable type, but is an improvement in the sense that the equivalent to None (Int:U) encodes its type.

In P6 all constructable types are automatically sum types with :U and :D subtypes.

> need to check for definedness

There is relatively little need for users to check definedness. The language and compiler ensure basic type/memory safety and the user can just write code and it'll automatically be type safe at this basic level.

When the need arises, the user only need type one or two characters in almost all cases (either `:D` if writing a type constraint or `if foo` or `?foo` if writing an explicit check).

> at run-time

In P6 definedness refers to the notion that something actually exists and this is deliberately aligned with the notion that something exists at run-time.

Users seldom need to concern themselves with this but when they do the P6 approach is simple and convenient.

> one of its major design blunders. Also this syntax!

I see the Int, Int:D, Int:U semantics and syntax as a design triumph. See another comment in this thread for an introduction to my view: https://news.ycombinator.com/item?id=18647590

> I rather use proper maybe types as in every other language.

When I read things like "proper" and "as in every other language" in this manner I experience them as appeals to authority.

Perl 6 is not a typical language. Just as Perl 5 isn't either.

For example, a P6 compiler is allowed to statically check that types match at compile time:

    sub foo (Str $bar) {}
    foo 42
yields the compile time error:

    Error while compiling .code.tio
    Calling foo(Int) will never work...
but it doesn't view static type checking as a panacea any more than it considers dynamic type checking as a panacea.

P6, like Perl, is designed around the notion of a this and that view of things rather than an either/or view.

Re: Haskell to Perl 6

#123
post #88
post #33

Earlier quoted context omitted.

Javascript has `I have ${n} apples`, or even `This is chapter ${i+1}.` In terms of interpolation, that's the variant that I like best.

Unlike Python's `"I have {} apples".format(n)` and various shells' `"I have $n apples"`, Javascript's and Apache Groovy's `"This is chapter ${i+1}"` mean the lexer must make a call to the parser when lexing the string contents -- this allows `"This is ${"chapter"} ${i+1}"` to parse. Don't know about Javascript, but this makes Groovy's parser unreadable, and stuck on a very old version of Antlr, i.e. 2 instead of 4 (t…

AFAIK, Groovy 3 is targeting Antlr 4.

Re: Haskell to Perl 6

#124
post #118

Earlier quoted context omitted.

> In P6, every type that's created using any of its type constructors is a sum type with two sub-types. > This includes types defined in the standard language such as Int. The sub-types are named D and U. > For example, to refer to Int's D sub-type, use `Int:D`. Probably your comment hints to this already in which case the following will be redundant but I wanted to mention them more explicitly. This is mainly for pe…

> Probably your comment hints to this already Regardless, I'm glad you've written your comment to clarify. :) I'd be delighted to hear about any of the several big or small mistakes I am about to make in the following. Also an honest opinion about whether the following approach I take boils down to one that is complete gibberish, vaguely interesting but terribly complex, OK but meh, good but a bit abstract, enlighten…

> ...the several big or small mistakes I am about to make in the following.

Everything you wrote seems fine to me and you even expounded on what I wrote! However, even if it wasn't, I'd be hardly pressed to come up with a response as clever and resourceful as your responses. For this reason, I quite enjoy reading your comments whenever I come across them on /r/perl6 and /r/programminglanguages.

> Like a lot of P6 stuff this... is both childishly simple but also so general, abstract, and high level... that it can be difficult to explain.

You're totally spot on. The most recent example of my struggle (mainly due to my ignorance of Perl 6 and probably CS concepts too) was the concept of containers and how they interface most assignments in Perl 6. I think the fact that many Perl 6 constructs/concepts, which complex in nature, seem so natural can deceive people who haven't tried it yet. This is because it's easy to overlook (or plainly ignore) how much thought went into both integrating them into the language and make them play nicely together.

---

Thanks for your comments!

Re: Haskell to Perl 6

#125
post #91
post #53

Earlier quoted context omitted.

That ought to be a oneliner in any sane language today: numbers.into_iter().filter(|n| n>10 && n%2!=0).for_each(work); map work . filter ((/=0) . (`mod` 2)) . filter (>10) $ numbers Insert tirade about how for-loops are free to do so much that they are slower-to-comprehend than specific-purpose iterator/list functions, here.

The language is happy to accommodate any style you like best, including oneliners. sub work($n) { say $n } my @numbers = 1..100; # rubyish @numbers.grep(* > 10 && * %% 2).map(&work); # lol turbo haskal map &work 10 grep * > 10 ==> grep * %% 2 ==> map &work; The traditional function composition operator exists, see https://docs.perl6.org/routine/%E2%88%98

The following doesn't do what you think it does

    * > 10 && * %% 2
That is two WhateverCode objects which each take one argument. Since both are definite it gives you the second one.

    my &a = * > 10;
    my &b = * %% 2;

    my &c = &a && &b;

    &c === &b; # True
If it wasn't split up by the `&&`, it would be a code object that took two arguments.

    # using multiplication (×) as a boolean and
    # (it always cooperates in the WhateverCode lambda syntax)
    my &c = (* > 10) × (* %% 2);

    say so c(10,2); # True

    say (1..20).grep(&c);
    # ((11 12) (13 14) (15 16) (17 18) (19 20))
Note that `grep` is written in terms of `map`

    (1..20).map({ ($^a,$^b) if  ($^a > 10) × ($^b %% 2) })
    # ((11 12) (13 14) (15 16) (17 18) (19 20))
If you need to refer to an argument more than once, you (generally) can't do it with the WhateverCode lambda syntax.

    ->   $n {  $n > 10 &&  $n %% 2 }
            { $^n > 10 && $^n %% 2 }
    sub ($n){  $n > 10 &&  $n %% 2 }
I say generally because array indexing will give you the number of elements for all the arguments you ask for.

    @a[ (* × ⅓) .. (* × ⅔) ]
    @a[ (@a.elems × ⅓) .. (@a.elems × ⅔) ]

Re: Haskell to Perl 6

#126

Earlier quoted context omitted.

The linguistic target Larry Wall aims at has little to do with uttering the line of code out loud.. The attempt to mirror human language is in the flexibility of the constructs. This is the much maligned There Is More Than One Way To Do It (TMTOWTDI) principle. The language is designed to allow the programmer to express themselves in they way that makes the point best for their brains. Now, this is almost a classical…

Hmm, interesting. TMTOWTDI is great for a small (one person) script, and One Way is better for a large (multi person) code base? That seems very plausible.

I think it is rather more related to team size than codebase size, really.

Think of how much can be done with a small group of people who all speak the same dialect / lingo. If everyone is on the same page WRT code style, then TMTOWTDI is just the magic that allowed you all to arrive on the same stylistic page.

Perl 6 is much better than Perl 5 in this because some of the styles that ossified in the Perl 5 dinosaur brains are truly grotesque to behold when deployed in the 21st century.

Re: Haskell to Perl 6

#127

Earlier quoted context omitted.

Duke Nukem Forever jokes, really? Look, Perl 6 can either be a "crazy complex" language that is "over-designed" and "hard to read", or it can some sort of toy with laughable complexity (your supposition is that Perl 6 is to elite programmers as Duke Nukem Forever is to elite gamers). So which is it? Complex enough for only elites to grasp? Or just some toy language with no real capabilities, let alone unique features…

I think you misunderstood or are trolling. This is a Haskell to Perl 6 guide. Haskell is for the elite programmers. Perl 6 is the Duke Nukem Forever of programming languages. This of course was all tongue in cheek.

Sorry dude, but the crux of your joke is that elite programmers shouldn't look at Perl 6.. because it took a while to develop?

And it is hardly "tongue in cheek" when you talk trash about the language (which you have never used) throughout this news item.

Re: Haskell to Perl 6

#128
post #121
post #119

Earlier quoted context omitted.

Thanks for the reply. Definitely looks like P6 can work at a higher level ...

You're welcome. Here's another quick example. It may or may not touch on your or the GGGP's point. So, no need to reply. I just thought I'd post something more while we wait for their reply. say now - INIT now This displays the time difference between the normal run-time moment that the `now` call before the minus is called and the `now` call after the minus sign which is run during the earlier INIT phase of executio…

>You're welcome. Here's another quick example. It may or may not touch on your or the GGGP's point. So, no need to reply.

I'll reply anyway, to say thanks again :)

I'll admit that I'm a bit phased by P6 phasers :) Had come across them just recently in the docs, initially thought, on a brief look, that at least the ENTER and LEAVE phasers were something like Python's __enter__ and __exit__ special methods used with context managers / "with" statements, or a way of wrapping a function call (or statement) in pre- and post-function invocations. which can be done with decorators in Python. But it seems like phasers may be something more, if not different. Also, there are many other kinds. Need to look into it some, including the "time traveling" code part.

Re: Haskell to Perl 6

#129
post #91

Earlier quoted context omitted.

The language is happy to accommodate any style you like best, including oneliners. sub work($n) { say $n } my @numbers = 1..100; # rubyish @numbers.grep(* > 10 && * %% 2).map(&work); # lol turbo haskal map &work 10 grep * > 10 ==> grep * %% 2 ==> map &work; The traditional function composition operator exists, see https://docs.perl6.org/routine/%E2%88%98

The following doesn't do what you think it does * > 10 && * %% 2 That is two WhateverCode objects which each take one argument. Since both are definite it gives you the second one. my &a = * > 10; my &b = * %% 2; my &c = &a && &b; &c === &b; # True If it wasn't split up by the `&&`, it would be a code object that took two arguments. # using multiplication (×) as a boolean and # (it always cooperates in the WhateverCo…

I noticed that error too but decided I didn't have the energy to provide a response worthy of the situation.

I'm so glad I didn't try. As always you've provided a helpful answer and I learned something new. I hadn't twigged that `[...]` context would treat each Whatever as the same value -- though in retrospect I can see of course it would given the Perlish principle of doing something very useful rather than doing something useless (generating an error).

I noticed what looked like a mistake in your post:

    my &c = (* > 10) × (* %% 2);

    say so c(10,2); # True
I thought "surely that returns False" and when I tried, it did.

Re: Haskell to Perl 6

#130
post #118

Earlier quoted context omitted.

> Probably your comment hints to this already Regardless, I'm glad you've written your comment to clarify. :) I'd be delighted to hear about any of the several big or small mistakes I am about to make in the following. Also an honest opinion about whether the following approach I take boils down to one that is complete gibberish, vaguely interesting but terribly complex, OK but meh, good but a bit abstract, enlighten…

> ...the several big or small mistakes I am about to make in the following. Everything you wrote seems fine to me and you even expounded on what I wrote! However, even if it wasn't, I'd be hardly pressed to come up with a response as clever and resourceful as your responses. For this reason, I quite enjoy reading your comments whenever I come across them on /r/perl6 and /r/programminglanguages. > Like a lot of P6 stu…

> Everything you wrote seems fine to me and you even expounded on what I wrote!

Given that your reply was just the right sort of clear answer that would make up for my original footnote I was especially happy to riff off your answer and get super detailed again. :)

> I'd be hardly pressed to come up with a response as clever and resourceful as your responses.

.oO ( Ever too clever by half )

> The most recent example of my struggle (mainly due to my ignorance of Perl 6 and probably CS concepts too) was the concept of containers and how they interface most assignments in Perl 6.

Yeah. Simple on the outside so noobs can just do their thing. Rich on the inside so gurus can develop new candy and gourmet meals.

Anyhoo, thanks for your reassuring feedback. :)

Post reply on HN