Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Yotam Barnoy <yotambarnoy@gmail.com>
To: Ashish Agarwal <agarwal1975@gmail.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Pattern matching on refs
Date: Thu, 10 Oct 2013 15:37:56 -0400	[thread overview]
Message-ID: <CAN6ygOmvhLO9L3TYvcJAozviwTSgaQ41Uv-rFLS05cy3FoB6-w@mail.gmail.com> (raw)
In-Reply-To: <CAMu2m2+9wtXm3ccK63_iVmWyQbJkJ1bVAr2nt2PB-zpg5Ss=jw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1387 bytes --]

It wouldn't solve the problem, because in reality I'm matching something
like this piece of code implementing a doubly-linked list:

type 'a cell = { data : 'a;
                 next : 'a link ref;
                 last : 'a link ref;
               }

and 'a link = Cons of 'a cell | Nil

type 'a t = {
  firstl: 'a link ref;
  lastl:  'a link ref;
}

  let pop s = match s with
    | {firstl = {contents=Nil}; lastl = _ } -> raise Empty

    | {lastl = {contents=Cons {data; _}}} when s.lastl == s.firstl ->
        (* one term *)
        s.lastl := Nil;
        s.firstl := Nil;
        data

    | {firstl =
      {contents=Cons {data; next={contents=Cons n}; last={contents=Nil}}}}
->
        n.last := Nil;
        s.firstl := Cons n;
        data
    | _ -> failwith "Error in pop"


See how unwieldy that is without the ability to clean it up by matching on
refs?

-Yotam


On Thu, Oct 10, 2013 at 3:34 PM, Ashish Agarwal <agarwal1975@gmail.com>wrote:

> Would it solve your problem to instead write:
>
> match !x with
> | y -> ...
>
>
> On Thu, Oct 10, 2013 at 3:17 PM, Yotam Barnoy <yotambarnoy@gmail.com>wrote:
>
>> I recently found out how ugly it is to pattern-match on a ref, using
>> {contents=...}. This should be extremely easy to fix in the parser. Can it
>> please be put into the next version of ocaml?
>> ie.
>>
>> match x with
>> | ref y -> ...
>>
>> -Yotam
>>
>
>

[-- Attachment #2: Type: text/html, Size: 2440 bytes --]

  reply	other threads:[~2013-10-10 19:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-10 19:17 Yotam Barnoy
2013-10-10 19:34 ` Ashish Agarwal
2013-10-10 19:37   ` Yotam Barnoy [this message]
2013-10-10 19:42 ` David Allsopp
2013-10-10 19:46   ` Yotam Barnoy
2013-10-11  4:49     ` Arnaud Spiwack
2013-10-11  6:10       ` Francois Berenger
2013-10-10 20:23 ` Jacques Le Normand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAN6ygOmvhLO9L3TYvcJAozviwTSgaQ41Uv-rFLS05cy3FoB6-w@mail.gmail.com \
    --to=yotambarnoy@gmail.com \
    --cc=agarwal1975@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox