|
List Info
Thread: "ocaml_beginners"::[] Simple match/constructor question
|
|
| "ocaml_beginners"::[] Simple
match/constructor question |
  France |
2007-06-06 03:20:42 |
|
Hi !
We can write :
(* --------------------------------- *)
type t = Chr of char
let tfn = function
Chr 'X' -> "O"
| _ -> "*"
(* --------------------------------- *)
However, this second code leads to a warning U :
"unused match case" ( for the non-joker case )
(* --------------------------------- *)
let xt1 = Chr 'X'
let tfn2 = function
xt1 -> "O"
| _ -> "*"
(* --------------------------------- *)
Warning that reveals the expected job will not be done.
Please, could you explain what happens with the second piece of code ?
Thanks.
Fabrice
--------------------------
These stunts are cunning !
La mule est en route.
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Simple match/constructor question |
  United States |
2007-06-06 05:35:19 |
|
On Wed, 6 Jun 2007, Fabrice Marchant wrote:
> Hi !
>
> We can write :
> (* --------------------------------- *)
> type t = Chr of char
>
> let tfn = function
> Chr 'X' -> "O"
> | _ -> "*"
> (* --------------------------------- *)
>
> However, this second code leads to a warning U :
> "unused match case" ( for the non-joker case )
> (* --------------------------------- *)
> let xt1 = Chr 'X'
>
> let tfn2 = function
> xt1 -> "O"
> | _ -> "*"
A lowercase identifier in a pattern (xt1 just above) is a catch-all, just
like _. The difference is that it gives a name to the corresponding value
so that you can use it later. There is no way of doing what you intended
in OCaml: patterns are defined at compile-time and are constant.
What you could do is:
let tfn2 = function
x when x = xt1 -> "O"
| _ -> "*"
It does the trick in some cases.
Martin
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Simple match/constructor question |
  France |
2007-06-06 14:11:56 |
|
Thanks a lot Martin for the explanations !
> A lowercase identifier in a pattern (xt1 just above) is a catch-all, just
> like _.
Please is there a summary for these lower/upper case rules ?
another example is : constructor begin with uppercase letter.
> The difference is that it gives a name to the corresponding value
> so that you can use it later. There is no way of doing what you intended
> in OCaml: patterns are defined at compile-time and are constant.
> What you could do is:
>
> let tfn2 = function
> x when x = xt1 -> "O"
> | _ -> "*"
>
> It does the trick in some cases.
Nice trick ! Ah, like the "as", I must remember to use these "when" too...
Regards.
Fabrice
--------
"Ouďr et cogiter"
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Simple match/constructor question |
  France |
2007-06-08 00:17:55 |
|
Thanks Frédéric !
> It's not really a new lower/upper case rule. The important lower/upper
> rule here is: first letter lowercase (or _) = variable.
OK.
> What's relevant is that a variable in pattern position (before "when")
> is always a catch-all; the variable is bound to the caught value.
Forgetting to speak about the module system, could we say these
lower/upper case rules are valid and enough for the core language ?
* constructors and constants begin with an uppercase
* all other words i.e. : keywords, type and variable names
begin with a lowercase
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Simple match/constructor question |
  United States |
2007-06-08 03:09:41 |
|
On Jun 8, 2007, at 1:17 AM, Fabrice Marchant wrote:
> Thanks Frédéric !
>
> > It's not really a new lower/upper case rule. The important lower/
> upper
> > rule here is: first letter lowercase (or _) = variable.
> OK.
>
> > What's relevant is that a variable in pattern position (before
> "when")
> > is always a catch-all; the variable is bound to the caught value.
>
> Forgetting to speak about the module system, could we say these
> lower/upper case rules are valid and enough for the core language ?
> * constructors and constants begin with an uppercase
> * all other words i.e. : keywords, type and variable names
> begin with a lowercase
Actually, I think the way it's it's enforced by the grammar is:
* Type Constructors (Variants & Polymorphic Variants), and Modules
must start with an upper case letter
* _All_ other identifiers (types, classes, bound identifiers) have to
start with a lowercase letter (or underscore)
--Jonathan
>
>
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Simple match/constructor question |
  France |
2007-06-08 01:53:30 |
|
> Actually, I think the way it's it's enforced by the grammar is:
> * Type Constructors (Variants & Polymorphic Variants), and Modules
> must start with an upper case letter
> * _All_ other identifiers (types, classes, bound identifiers) have to
> start with a lowercase letter (or underscore)
Thanks Jonathan for your accurate answer !
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
Simple match/constructor question |
  United States |
2007-06-08 10:21:49 |
|
On Fri, 8 Jun 2007, Jonathan Bryant wrote:
> Actually, I think the way it's it's enforced by the grammar is:
> * Type Constructors (Variants & Polymorphic Variants), and Modules
> must start with an upper case letter
> * _All_ other identifiers (types, classes, bound identifiers) have to
> start with a lowercase letter (or underscore)
You can actually start polymorphic variants with lower case letters:
# type lock = [`off | `on];;
type lock = [ `off | `on ]
However, as noted in the manual:
"Note on variant tags: the current implementation accepts lowercase
variant tags in addition to uppercase variant tags, but we suggest you
avoid lowercase variant tags for portability and compatibility with future
OCaml versions."
William D. Neumann
---
"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.
Tigers are noble and sleek; children are loud and messy."
-- Neko Case
Life is unfair. Kill yourself or get over it.
-- Black Box Recorder
__._,_.___
.
__,_._,___
|
[1-7]
|
|