caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] naming parts of optional arguments?
@ 2003-03-28  1:05 Chris Hecker
  2003-03-28  1:10 ` Chris Hecker
  2003-03-28  1:43 ` Max Kirillov
  0 siblings, 2 replies; 10+ messages in thread
From: Chris Hecker @ 2003-03-28  1:05 UTC (permalink / raw)
  To: caml-list


Is there a way to name the components of an optional argument?

# let f ((x,y) as xy) = xy;;
val f : 'a * 'b -> 'a * 'b = <fun>
# let f ?xy () = xy;;
val f : ?xy:'a -> unit -> 'a option = <fun>
# let f ?((x,y) as xy) () = xy;;
Characters 8-9:
   let f ?((x,y) as xy) () = xy;;
           ^
Syntax error

Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-03-28  1:05 [Caml-list] naming parts of optional arguments? Chris Hecker
@ 2003-03-28  1:10 ` Chris Hecker
       [not found]   ` <8F4A019E-60BD-11D7-829E-000393BA7EBA@wetware.com>
  2003-03-28  1:43 ` Max Kirillov
  1 sibling, 1 reply; 10+ messages in thread
From: Chris Hecker @ 2003-03-28  1:10 UTC (permalink / raw)
  To: caml-list


Before someone comments that the optional argument is an 'a option and not 
an 'a, what I really wanted to do is this:

# let f ?(((x,y) as xy) = 0.0,0.0) () = xy;;
Characters 8-9:
   let f ?(((x,y) as xy) = 0.0,0.0) () = xy;;
           ^
Syntax error

Which also doesn't work but there's no 'a option involved, so it 
"should".  I was trying to create the shortest example but took it a bit 
too far.

Chris

At 17:05 3/27/2003 -0800, Chris Hecker wrote:

>Is there a way to name the components of an optional argument?
>
># let f ((x,y) as xy) = xy;;
>val f : 'a * 'b -> 'a * 'b = <fun>
># let f ?xy () = xy;;
>val f : ?xy:'a -> unit -> 'a option = <fun>
># let f ?((x,y) as xy) () = xy;;
>Characters 8-9:
>   let f ?((x,y) as xy) () = xy;;
>           ^
>Syntax error
>
>Chris
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-03-28  1:05 [Caml-list] naming parts of optional arguments? Chris Hecker
  2003-03-28  1:10 ` Chris Hecker
@ 2003-03-28  1:43 ` Max Kirillov
  1 sibling, 0 replies; 10+ messages in thread
From: Max Kirillov @ 2003-03-28  1:43 UTC (permalink / raw)
  To: caml-list

On Thu, Mar 27, 2003 at 05:05:17PM -0800, Chris Hecker wrote:
> 
> Is there a way to name the components of an optional argument?

# let f ?xy:((x,y)=0,0) () = x+y;; 
val f : ?xy:int * int -> unit -> int = <fun>
# f ();;                           
- : int = 0
# f ~xy:(1,2) ();;
- : int = 3

-- 
Max

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
       [not found]   ` <8F4A019E-60BD-11D7-829E-000393BA7EBA@wetware.com>
@ 2003-03-28  2:11     ` Chris Hecker
  2003-07-28  3:41       ` Chris Hecker
  0 siblings, 1 reply; 10+ messages in thread
From: Chris Hecker @ 2003-03-28  2:11 UTC (permalink / raw)
  To: caml-list


James replied off list, but I thought the others might like to see it:

At 17:35 3/27/2003 -0800, james woodyatt wrote:
># let f ?xy:(x,y as xy = 0.0,0.0) () = xy;;
>val f : ?xy:float * float -> unit -> float * float = <fun>

Ah, that's what I wanted, thanks!  It looks like you can just do ?xy:(x,y = 
0,0) and skip the "as xy" too.  Wacky syntax...it looks like I should have 
looked in the pseudo-bnf before posting since it looks like this is in there.

Thanks,
Chris


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-03-28  2:11     ` Chris Hecker
@ 2003-07-28  3:41       ` Chris Hecker
  2003-07-28  7:18         ` Jacques Garrigue
  0 siblings, 1 reply; 10+ messages in thread
From: Chris Hecker @ 2003-07-28  3:41 UTC (permalink / raw)
  To: caml-list


[old thread resurrected]

Actually, I spoke too soon below, you have to have the "as xy" on there to 
be able to reference the tuple by xy.  Doesn't that seem redundant given 
the ?xy: specification (which doesn't seem to be used)?

# let f ?xy:((x,y)=(0,0)) () = x + y;;
val f : ?xy:int * int -> unit -> int = <fun>
# let f ?xy:((x,y)=(0,0)) () = xy;;
Characters 29-31:
   let f ?xy:((x,y)=(0,0)) () = xy;;
                                ^^
Unbound value xy
# let f ?xy:((x,y) as xy =(0,0)) () = xy;;
val f : ?xy:int * int -> unit -> int * int = <fun>
# let f ?xy:((x,y) as blah =(0,0)) () = blah;;
val f : ?xy:int * int -> unit -> int * int = <fun>
#

Chris



At 18:11 3/27/2003 -0800, Chris Hecker wrote:

>James replied off list, but I thought the others might like to see it:
>
>At 17:35 3/27/2003 -0800, james woodyatt wrote:
>># let f ?xy:(x,y as xy = 0.0,0.0) () = xy;;
>>val f : ?xy:float * float -> unit -> float * float = <fun>
>
>Ah, that's what I wanted, thanks!  It looks like you can just do ?xy:(x,y 
>= 0,0) and skip the "as xy" too.  Wacky syntax...it looks like I should 
>have looked in the pseudo-bnf before posting since it looks like this is 
>in there.
>
>Thanks,
>Chris
>
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-07-28  3:41       ` Chris Hecker
@ 2003-07-28  7:18         ` Jacques Garrigue
  2003-07-28 16:35           ` Chris Hecker
  0 siblings, 1 reply; 10+ messages in thread
From: Jacques Garrigue @ 2003-07-28  7:18 UTC (permalink / raw)
  To: checker; +Cc: caml-list

From: Chris Hecker <checker@d6.com>
Date: Sun, 27 Jul 2003 20:41:57 -0700

> [old thread resurrected]

Very old indeed.

> Actually, I spoke too soon below, you have to have the "as xy" on there to 
> be able to reference the tuple by xy.  Doesn't that seem redundant given 
> the ?xy: specification (which doesn't seem to be used)?
> 
> # let f ?xy:((x,y)=(0,0)) () = x + y;;
> val f : ?xy:int * int -> unit -> int = <fun>
> # let f ?xy:((x,y)=(0,0)) () = xy;;
> Characters 29-31:
>    let f ?xy:((x,y)=(0,0)) () = xy;;
>                                 ^^
> Unbound value xy
> # let f ?xy:((x,y) as xy =(0,0)) () = xy;;
> val f : ?xy:int * int -> unit -> int * int = <fun>
> # let f ?xy:((x,y) as blah =(0,0)) () = blah;;
> val f : ?xy:int * int -> unit -> int * int = <fun>

No contradiction at all: ?xy: is a label, not a variable name.
If you care about length, you can still write:

  let f ?(xy=(0,0)) () = xy

The distinction between label and variable is useful to avoid masking
the environment.

  let x = ref 3
  let f ?x:(x' = !x + 1) () = x := x'

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-07-28  7:18         ` Jacques Garrigue
@ 2003-07-28 16:35           ` Chris Hecker
  2003-07-29  3:19             ` Jacques Garrigue
  0 siblings, 1 reply; 10+ messages in thread
From: Chris Hecker @ 2003-07-28 16:35 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list


>No contradiction at all: ?xy: is a label, not a variable name.

Well, except for in this case, of course, when it is a variable name:

# let f ?xy () = xy;;
val f : ?xy:'a -> unit -> 'a option = <fun>

>If you care about length, you can still write:
>   let f ?(xy=(0,0)) () = xy

No, because I need to break out the parts of the tuple and name the whole 
tuple, which was the point of the thread, so the only way to do that I can 
see is this beautiful expression:

# let f ?xy:((x,y) as xy = 0,0) () = xy,x,y;;
val f : ?xy:int * int -> unit -> (int * int) * int * int = <fun>

All I'm saying is that having the first example above work leads one to 
believe the xy is usable inside the function body in general.

On the whole, the syntax for defining default parameters is nonintuitive 
and inconsistent, because you need the parenthesis sometimes, you need the 
"as xy" sometimes, you need the ?label: sometimes, etc.

Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-07-28 16:35           ` Chris Hecker
@ 2003-07-29  3:19             ` Jacques Garrigue
  2003-07-29 21:52               ` Chris Hecker
  0 siblings, 1 reply; 10+ messages in thread
From: Jacques Garrigue @ 2003-07-29  3:19 UTC (permalink / raw)
  To: checker; +Cc: caml-list

From: Chris Hecker <checker@d6.com>
Subject: Re: [Caml-list] naming parts of optional arguments?
Date: Mon, 28 Jul 2003 09:35:15 -0700

> 
> >No contradiction at all: ?xy: is a label, not a variable name.
> 
> Well, except for in this case, of course, when it is a variable name:
> 
> # let f ?xy () = xy;;
> val f : ?xy:'a -> unit -> 'a option = <fun>

Where do you see a ?xy: in the above source code?
?xy is a shorthand for ?xy:xy, as explained in the manual.
xy is the variable, not ?xy:.

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
  2003-07-29  3:19             ` Jacques Garrigue
@ 2003-07-29 21:52               ` Chris Hecker
  0 siblings, 0 replies; 10+ messages in thread
From: Chris Hecker @ 2003-07-29 21:52 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list


>Where do you see a ?xy: in the above source code?
>?xy is a shorthand for ?xy:xy, as explained in the manual.
>xy is the variable, not ?xy:.

Ah, my mistake.  Although, you must admit, it's pretty subtle.  That still 
doesn't answer why the "as" doesn't work without the label.

Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] naming parts of optional arguments?
@ 2003-03-28  1:32 Matt Gushee
  0 siblings, 0 replies; 10+ messages in thread
From: Matt Gushee @ 2003-03-28  1:32 UTC (permalink / raw)
  To: Chris Hecker

On 27 Mar 2003 at 17:05, Chris Hecker wrote:
 
> Is there a way to name the components of an optional argument?

Are you sure that's what you need to do? Your examples don't show you 
using the x and y values separately.

> # let f ((x,y) as xy) = xy;;
> val f : 'a * 'b -> 'a * 'b = <fun>
> # let f ?xy () = xy;;
> val f : ?xy:'a -> unit -> 'a option = <fun>
> # let f ?((x,y) as xy) () = xy;;
> Characters 8-9:
>    let f ?((x,y) as xy) () = xy;;

At any rate, this works for me:

  # let f ?(xy = 0.0, 0.0) () = xy;;
  val f : ?xy:float * float -> unit -> float * float = <fun>

Now you know xy is a pair of floats, and it's simple to extract the 
separate values if you need to. Or do you need something else that 
I've missed?

-- 
Matt Gushee
Englewood, CO USA

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-07-29 21:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-28  1:05 [Caml-list] naming parts of optional arguments? Chris Hecker
2003-03-28  1:10 ` Chris Hecker
     [not found]   ` <8F4A019E-60BD-11D7-829E-000393BA7EBA@wetware.com>
2003-03-28  2:11     ` Chris Hecker
2003-07-28  3:41       ` Chris Hecker
2003-07-28  7:18         ` Jacques Garrigue
2003-07-28 16:35           ` Chris Hecker
2003-07-29  3:19             ` Jacques Garrigue
2003-07-29 21:52               ` Chris Hecker
2003-03-28  1:43 ` Max Kirillov
2003-03-28  1:32 Matt Gushee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).