Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml - Pattern matching with list reference in a tuple

is there a cleaner way of doing this? I'm trying to do pattern matching of a

(a' option * (char * nodeType) list ref

the only way I found was doing this :

match a with
| _, l -> match !l with
  | (c, n)::t -> doSomething 

Wouldn't there be a way to match a with something else like ...

match a with
| _, ref (c,n)::t -> doSomething

... or something similar? In this example it doesn't look heavy to just do another match with, but in the real case it can somewhat be...

Thanks for your answers.

like image 890
Pacane Avatar asked Apr 25 '12 03:04

Pacane


2 Answers

The ref type is defined as a record with a mutable field:

type 'a ref = {
    mutable contents : 'a;
}

This means that you can pattern match against it using record syntax like this:

match a with
| _, { contents = (c,n)::t } -> doSomething
like image 146
sepp2k Avatar answered Sep 20 '22 12:09

sepp2k


In OCaml a ref is secretly a record with a mutable field named contents.

match a with
| _, { contents = (c, n) :: t } -> (* Do something *)
like image 36
Jeffrey Scofield Avatar answered Sep 18 '22 12:09

Jeffrey Scofield