Re: [Haskell-cafe] Prolog-style list syntax?
Anthony Clayden <[email protected]> Wed, 7 Jul 2021 18:36:07 +1200
| Newsgroups | gmane.comp.lang.haskell.hugs.user,gmane.comp.lang.haskell.cafe |
|---|---|
| Message-ID | <CABU_mxhud6DY6R65tqeL77VEvu9tYajKUyu+ERy96dhE6Zg5tQ@mail.gmail.com> |
--===============5053001934339896491== Content-Type: multipart/alternative; boundary="0000000000006b8ead05c682c16a" --0000000000006b8ead05c682c16a Content-Type: text/plain; charset="UTF-8" Ok so I implemented both ideas: > elem2 _ [] = False > elem2 x [y,: ys] = x == y || elem2 x ys > let [x,y,z,w,: ws] = "hello there" in ws -- yields "o there" In fact it was easier to implement both ideas than make a special case of `(:)`. So these work > let [x,:+ xs] = [1,2,3,:+ Nily] in xs > let [x,`ConsSet` xs] = [1,2,3,`ConsSet` NilSet] in xs with decls > infixr 5 :+ > data Listy a = Nily | a :+ (Listy a) deriving (Eq, Show, Read) > infixr 5 `ConsSet` > data Set a = NilSet | ConsSet a (Set a) deriving (Eq, Show, Read) On Wed, 30 Jun 2021 at 19:00, Anthony Clayden <[email protected]> wrote: > Ok thank you for the feedback, I get the message not to re-purpose > currently valid syntax. > > > > [x, y ,: ys ] -- ? not currently valid > > We could make list syntax work harder > > > [x, y ,:+ ys ] > > Means desugar the commas by applying constructor `:+` instead of `:`. That > could be in general any constructor starting `:`. > > Or indeed could be a pattern synonym starting `:`, which is a 'smart > constructor' to build the list maintaining some invariant. > > > --0000000000006b8ead05c682c16a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div>Ok so I implemented both ideas:</div><div><br></div><= div>>=C2=A0 =C2=A0=C2=A0<span style=3D"font-family:Arial,Helvetica,sans-= serif">=C2=A0elem2 _ [] =C2=A0 =C2=A0 =C2=A0 =3D False</span></div>>=C2= =A0 =C2=A0 elem2 x [y,: ys] =3D x =3D=3D y || elem2 x ys<div><font face=3D"= Arial, Helvetica, sans-serif"><br></font></div><div><font face=3D"Arial, He= lvetica, sans-serif">>=C2=A0 =C2=A0 let [x,y,z,w,: ws] =3D "hello t= here" in ws -- yields "o there"</font></div><div><font face= =3D"Arial, Helvetica, sans-serif"><br></font></div><div><font face=3D"Arial= , Helvetica, sans-serif">In fact it was easier to implement both ideas than= make a special case of `(:)`. So these work</font></div><div><font face=3D= "Arial, Helvetica, sans-serif"><br></font></div><div><font face=3D"Arial, H= elvetica, sans-serif">>=C2=A0 =C2=A0=C2=A0</font>let [x,:+ xs] =3D [1,2,= 3,:+ Nily] in xs</div><div><br></div><div>> let [x,`ConsSet` xs] =3D [1,= 2,3,`ConsSet` NilSet] in xs<br><br>with decls</div><div><br></div><div>>= <span style=3D"font-family:Arial,Helvetica,sans-serif">=C2=A0 =C2=A0 infixr= 5 :+</span></div>>=C2=A0 =C2=A0 data Listy a =3D Nily | a :+ (Listy a) = =C2=A0deriving (Eq, Show, Read)<br><div><br></div><div>>=C2=A0=C2=A0 =C2= =A0 infixr 5 `ConsSet`<br>>=C2=A0 =C2=A0 data Set a =3D NilSet | ConsSet= a (Set a) =C2=A0deriving (Eq, Show, Read)<br><br><br><div class=3D"gmail_q= uote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, 30 Jun 2021 at 19:00, A= nthony Clayden <<a href=3D"mailto:[email protected]">anthony.d= [email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_quote= " style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);= padding-left:1ex"><div dir=3D"ltr">Ok thank you for the feedback, I get the= message not to re-purpose currently valid syntax.<div><br></div><div><br><= /div><div>>=C2=A0 =C2=A0 [x, y ,: ys ]=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0-- ? not currently valid</div><div><br></div><div>We could make list= syntax work harder</div><div><br></div><div>>=C2=A0 =C2=A0 [x, y ,:+ ys= ]</div><div><br></div><div>Means desugar the commas by applying constructo= r `:+` instead of `:`. That could be in general any constructor starting `:= `.</div><div><br></div><div>Or indeed could be a pattern synonym starting `= :`, which is a 'smart constructor' to build the list maintaining so= me invariant.</div><div><br></div><div><br></div></div> </blockquote></div></div></div> --0000000000006b8ead05c682c16a-- --===============5053001934339896491== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KSHVncy1Vc2Vy cyBtYWlsaW5nIGxpc3QKSHVncy1Vc2Vyc0BoYXNrZWxsLm9yZwpodHRwOi8vbWFpbC5oYXNrZWxs Lm9yZy9jZ2ktYmluL21haWxtYW4vbGlzdGluZm8vaHVncy11c2Vycwo= --===============5053001934339896491==--