[Haskell-cafe] Prolog-style patterns
Joachim Breitner
mail at joachim-breitner.de
Mon Apr 8 16:59:42 CEST 2013
Hi,
I believe one problem with non-linear patterns would be that the
compiler has to figure out what notion of equality you want here. An
obvious choice is (==), but the Eq instances might not do what you want.
Using pattern guards or view patterns explicates this choice.
Also, without an explicit call to == the cost model of such a function
definition might be harder to see; an innocent looking change to the
patterns of a function could cause a considerable amount of extra work
if == is expensive.
Greetings,
Joachim
Am Montag, den 08.04.2013, 07:06 -0700 schrieb Conal Elliott:
> Hi Jan,
>
> What you're suggesting is called "non-linear patterns", and it's a
> perfectly sensible, well-defined feature in a language with
> pattern-matching. As you point out, non-linearity allows for more
> direct & succinct programming. I've often wished for this feature when
> writing optimizations on data types, especially for syntactic types
> (languages).
>
> As Ivan mentioned, there is some danger that people may accidentally a
> non-linear pattern accidentally, and perhaps the early Haskell
> designers chose the linearity restriction out of this worry. The
> importance of such dangers is a subjective call, and certainly not one
> carried out consistently in Haskell. Consider, for instance, the
> choice that let & where bindings are recursive by default in Haskell,
> unlike ML and Lisp. I like this choice, but I can understand
> objections that it leads to accidental recursions, especially for
> non-functions.
>
>
>
> -- Conal
>
>
>
>
> On Mon, Apr 8, 2013 at 6:11 AM, Jan Stolarek <jan.stolarek at p.lodz.pl>
> wrote:
> > You can achieve something similar with the ViewPatterns
> language
> > extension.
> >
>
> > member _ [] = False
> > member x (((x ==) -> True) : _) = True
> > member x (_ : xs) = member x xs
>
> Hi Tillmann,
>
> there are a couple of ways to achieve this in Haskell, for
> example using guards:
>
> member :: Eq a => a -> [a] -> Bool
> member _ [] = False
>
> member y (x:_) | x == y = True
> member y (_:xs) = member y xs
>
> The goal of my proposal is to provide a concise syntax,
> whereas ViewPatterns are very verbose and
> guards are slightly verbose. I want something simple and
> something that is very intuitive if
> you've programmed in Prolog :)
>
> Janek
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
--
Joachim "nomeata" Breitner
Debian Developer
nomeata at debian.org | ICQ# 74513189 | GPG-Keyid: 4743206C
JID: nomeata at joachim-breitner.de | http://people.debian.org/~nomeata
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130408/d6c888f8/attachment.pgp>
More information about the Haskell-Cafe
mailing list