* [Caml-list] optional arguments
@ 2003-11-17 8:52 Selentek 24331-03
2003-11-17 10:13 ` Richard Jones
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Selentek 24331-03 @ 2003-11-17 8:52 UTC (permalink / raw)
To: caml-list
Hello,
Is there any way to "send optional argument with None value".
for example:
let a = None;;
val a : 'a option = None
let f ?v () =
match v with
| Some i -> i + 1
| None -> 0
.
First solution:
match a with
| Some i -> f ~v:i ()
| None -> f ()
Can I find something more easy with out redifinition of the function ?
something like: f a (), but funtion defined by ``let f ?v'' and ``a'' is int option.
Thanks.
Sorry for my english.
--
-------------------
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] 7+ messages in thread
* Re: [Caml-list] optional arguments
2003-11-17 8:52 [Caml-list] optional arguments Selentek 24331-03
@ 2003-11-17 10:13 ` Richard Jones
2003-11-17 10:44 ` Selentek 24331-03
2003-11-17 10:13 ` Artem Prisyznuk
2003-11-17 17:24 ` Brian Hurt
2 siblings, 1 reply; 7+ messages in thread
From: Richard Jones @ 2003-11-17 10:13 UTC (permalink / raw)
To: Selentek 24331-03; +Cc: caml-list
On Mon, Nov 17, 2003 at 11:52:49AM +0300, Selentek 24331-03 wrote:
[...]
Like this?
# let f ?v () =
match v with
| Some i -> i + 1
| None -> 0 ;;
val f : ?v:int -> unit -> int = <fun>
# let v = Some 1;;
val v : int option = Some 1
# f ?v ();;
- : int = 2
# let v = None;;
val v : 'a option = None
# f ?v ();;
- : int = 0
Rich.
--
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MONOLITH is an advanced framework for writing web applications in C, easier
than using Perl & Java, much faster and smaller, reusable widget-based arch,
database-backed, discussion, chat, calendaring:
http://www.annexia.org/freeware/monolith/
-------------------
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] 7+ messages in thread
* Re: [Caml-list] optional arguments
2003-11-17 8:52 [Caml-list] optional arguments Selentek 24331-03
2003-11-17 10:13 ` Richard Jones
@ 2003-11-17 10:13 ` Artem Prisyznuk
2003-11-17 17:24 ` Brian Hurt
2 siblings, 0 replies; 7+ messages in thread
From: Artem Prisyznuk @ 2003-11-17 10:13 UTC (permalink / raw)
To: Selentek 24331-03; +Cc: caml-list
On Mon, 17 Nov 2003 11:52:49 +0300, Selentek 24331-03 <epifanov@komset.ru>
wrote:
> Is there any way to "send optional argument with None value".
f ?v:a ();;
> let a = None;;
> val a : 'a option = None
>
> let f ?v () =
> match v with
> | Some i -> i + 1
> | None -> 0
--
Artem Prysyznuk
tema@sit.kiev.ua
-------------------
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] 7+ messages in thread
* Re: [Caml-list] optional arguments
2003-11-17 10:13 ` Richard Jones
@ 2003-11-17 10:44 ` Selentek 24331-03
0 siblings, 0 replies; 7+ messages in thread
From: Selentek 24331-03 @ 2003-11-17 10:44 UTC (permalink / raw)
To: Richard Jones; +Cc: Selentek 24331-03, caml-list
YES! Thanks for information.
On 10:13 Mon 17 Nov , Richard Jones wrote:
> On Mon, Nov 17, 2003 at 11:52:49AM +0300, Selentek 24331-03 wrote:
> [...]
>
> Like this?
>
> # let f ?v () =
> match v with
> | Some i -> i + 1
> | None -> 0 ;;
> val f : ?v:int -> unit -> int = <fun>
>
> # let v = Some 1;;
> val v : int option = Some 1
>
> # f ?v ();;
> - : int = 2
>
> # let v = None;;
> val v : 'a option = None
>
> # f ?v ();;
> - : int = 0
>
> Rich.
>
> --
> Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
> Merjis Ltd. http://www.merjis.com/ - improving website return on investment
> MONOLITH is an advanced framework for writing web applications in C, easier
> than using Perl & Java, much faster and smaller, reusable widget-based arch,
> database-backed, discussion, chat, calendaring:
> http://www.annexia.org/freeware/monolith/
--
Gentoo Linux http://www.gentoo.org
-------------------
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] 7+ messages in thread
* Re: [Caml-list] optional arguments
2003-11-17 8:52 [Caml-list] optional arguments Selentek 24331-03
2003-11-17 10:13 ` Richard Jones
2003-11-17 10:13 ` Artem Prisyznuk
@ 2003-11-17 17:24 ` Brian Hurt
2 siblings, 0 replies; 7+ messages in thread
From: Brian Hurt @ 2003-11-17 17:24 UTC (permalink / raw)
To: Selentek 24331-03; +Cc: caml-list
On Mon, 17 Nov 2003, Selentek 24331-03 wrote:
> Hello,
>
> Is there any way to "send optional argument with None value".
> for example:
>
> let a = None;;
> val a : 'a option = None
If you delete the above two lines...
>
> let f ?v () =
> match v with
> | Some i -> i + 1
> | None -> 0
>
This code just does what you want it to. It has the type:
val f : ?v:int -> unit -> int = <fun>
And is called like:
f ();; (* returns 0 *)
f ~v:1 () ;; (* returns 2 *)
Another possibility is to do:
let f ?(v = None) () =
match v with
None -> 0
| Some i -> i + 1
;;
This has the type:
val f : ?v:int option -> unit -> int = <fun>
And is called like:
f ();; (* returns 0 *)
f ~v:None ();; (* returns 0 *)
f ~v:(Some 1) ();; (* returns 2 *)
Form #2 is probably want you want to do if you just want to pass in an
option.
>
> Sorry for my english.
>
Your english is as good as mine :-}.
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian
-------------------
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] 7+ messages in thread
* Re: [Caml-list] Optional arguments
2008-11-09 19:51 ` [Caml-list] " David Teller
@ 2008-11-10 20:52 ` malc
0 siblings, 0 replies; 7+ messages in thread
From: malc @ 2008-11-10 20:52 UTC (permalink / raw)
To: David Teller; +Cc: caml-list
On Sun, 9 Nov 2008, David Teller wrote:
> What were you expecting?
>
> Your definition of [a] always passes argument [i] to [b], so the default
> value is never used.
Yes indeed, i probably should quit writing code this late, sorry for
the noise.
--
mailto:av1474@comtv.ru
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Optional arguments
2008-11-09 19:17 Optional arguments malc
@ 2008-11-09 19:51 ` David Teller
2008-11-10 20:52 ` malc
0 siblings, 1 reply; 7+ messages in thread
From: David Teller @ 2008-11-09 19:51 UTC (permalink / raw)
To: malc; +Cc: caml-list
What were you expecting?
Your definition of [a] always passes argument [i] to [b], so the default
value is never used.
Cheers,
David
On Sun, 2008-11-09 at 22:17 +0300, malc wrote:
> Objective Caml version 3.10.0
>
> # let a i = let b ?(i=i mod 3) () = i in b ~i ();;
> val a : int -> int = <fun>
> # for i = 0 to 5 do print_int (a i); done;;
> 012345- : unit = ()
>
> Is this something to be expected? Or perhaps something which calls
> for an upgrade?
>
--
David Teller-Rajchenbach
Security of Distributed Systems
http://www.univ-orleans.fr/lifo/Members/David.Teller
Angry researcher: French Universities need reforms, but the LRU act brings liquidations.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2008-11-10 20:52 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-17 8:52 [Caml-list] optional arguments Selentek 24331-03
2003-11-17 10:13 ` Richard Jones
2003-11-17 10:44 ` Selentek 24331-03
2003-11-17 10:13 ` Artem Prisyznuk
2003-11-17 17:24 ` Brian Hurt
2008-11-09 19:17 Optional arguments malc
2008-11-09 19:51 ` [Caml-list] " David Teller
2008-11-10 20:52 ` malc
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox