February 14, 2014 Re: Implementing Haskell's Type-Level Quicksort in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Fri, Feb 14, 2014 at 3:54 AM, Meta <jared771@gmail.com> wrote:
> It seems strange that it would choke now, as Cons is a struct. Therefore, Cons!(Three, ...) should create a new type, and `L: Cons!(a, b), a, b` shouldn't be any trouble to destructure into two types, `Three` and `Cons!(Two, ...)`. It had no problem handling the Succ and Number struct templates I defined.
`alias` is just a bit of syntax sugar, it does not (at least for
2.064) have the same power than fully defining a template and the
`is(...)` expression.
So yes, D does not have Haskell nice syntax for pattern matching. You can do some pattern matching using templates, but it tends to be a bit heavier than ML/F#/Haskell.
(Some would say that at least D does not force you to encode numbers as succ/zero list, since you can use numbers directly as template args, but that's another story).
|
February 14, 2014 Re: Implementing Haskell's Type-Level Quicksort in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | Philippe Sigaud: > So yes, D does not have Haskell nice syntax for pattern matching. I'd like some of such syntax for templates (and a little different syntax added to match structs inside switch statements: https://d.puremagic.com/issues/show_bug.cgi?id=596 ). Bye, bearophile |
February 14, 2014 Re: Implementing Haskell's Type-Level Quicksort in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On Friday, 14 February 2014 at 06:05:08 UTC, Philippe Sigaud wrote: > `alias` is just a bit of syntax sugar, it does not (at least for > 2.064) have the same power than fully defining a template and the `is(...)` expression. Right. What I was saying, however, is it is strange to me that this code will compile: alias numPred(N: Succ!a, a) = a; struct Number(a) if (is(a == Zero) || is(a == Succ!x, x)) {} enum numValue(N: Number!Zero) = 0; enum numValue(N: Number!x, x) = numValue!(Number!(numPred!x)) + 1; assert(numValue!(Number!Zero) == 0); assert(numValue!(Number!Four) == 4); While this code won't: struct Nil {} struct Cons(a, b) {} alias list1 = Cons!(Three, Cons!(Two, Cons!(Four, Cons!(One, Nil)))); alias numlHead(L: Cons!(a, b), a, b) = a; alias numlTail(L: Cons!(a, b), a, b) = b; assert(is(numlHead!list1 == Three)); Since list1 is a roughly similar construction to Number!(Succ!(Succ!(Succ!(Succ!Zero)))), it is surprising to me that the template matching system can correctly destructure the latter, while not the former. This is obviously a bug, I'm just surprised that it exists. > So yes, D does not have Haskell nice syntax for pattern matching. You can do some pattern matching using templates, > but it tends to be a bit heavier than ML/F#/Haskell. While it is heavier than Haskell's syntax, I have been consistently and pleasantly surprised by how powerful D's template pattern matching is (bugs notwithstanding). I wonder how well-known this is outside this mailing list... > (Some would say that at least D does not force you to encode numbers as succ/zero list, since you can use numbers directly as template args, but that's another story). Sure, but I want to stay as close to the original Haskell as possible (and Peano Numbers are pretty damn cool anyway). |
February 14, 2014 Re: Implementing Haskell's Type-Level Quicksort in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | Meta:
> While it is heavier than Haskell's syntax, I have been consistently and pleasantly surprised by how powerful D's template pattern matching is (bugs notwithstanding). I wonder how well-known this is outside this mailing list...
I keep reading blog posts that use Haskell and present supposed "marvels", using very complex things, that can be routinely done in D, and with less complexity for the brain of the programer, often leading to faster code and equally safe :-) So while I like several parts of Haskell, it's sometimes over-hyped (while D is nearly invisible).
Bye,
bearophile
|
February 14, 2014 Re: Implementing Haskell's Type-Level Quicksort in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Fri, Feb 14, 2014 at 3:24 PM, bearophile <bearophileHUGS@lycos.com> wrote: > Meta: > > >> While it is heavier than Haskell's syntax, I have been consistently and pleasantly surprised by how powerful D's template pattern matching is (bugs notwithstanding). I wonder how well-known this is outside this mailing list... Well, I have a tutorial, but I admit it being somewhat incomplete and now partially out of date (2012). > I keep reading blog posts that use Haskell and present supposed "marvels", using very complex things, that can be routinely done in D, and with less complexity for the brain of the programer, often leading to faster code and equally safe :-) So while I like several parts of Haskell, it's sometimes over-hyped (while D is nearly invisible). Same here, same here! But you have to admit some of their examples are pretty damn elegant :) I remember doing some Haskell -> D translation (a bit like what Meta is doing), making it work, doing the happy-happy dance, and then suddenly realizing I could do the same compile-time computation in one line of D ;) |
Copyright © 1999-2021 by the D Language Foundation