* [Caml-list] syntax for variable arity?
@ 2001-08-30 21:45 Michael Leary
2001-08-31 7:35 ` Remi VANICAT
0 siblings, 1 reply; 2+ messages in thread
From: Michael Leary @ 2001-08-30 21:45 UTC (permalink / raw)
To: caml
I want to write a function to abstract a series of functions with different
numbers of args like these two:
let debug message = printf "Debug %s\n" message; fso
let debug_line start_angle start_radius end_angle end_radius =
printf "DebugLine %f %f %f %f\n" start_angle start_radius end_angle end_radius;
fso
I was thinking of:
type command =
Debug
| Debug_line
| ...
let com command _ =
match command with
| Debug ->
doit "some special format string" _
| Debug_line ->
doit "other format string" _
let doit _ = printf _ ; fso (* fso defined elsewhere *)
let () = com Debug "debug message"
...
let () = com Debug_line start_angle start_radius end_angle end_radius
I know _ matches any value(s??), but I'm not clear on how to use the
match elsewhere... am I close? Is there a better way to structure this?
--
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] syntax for variable arity?
2001-08-30 21:45 [Caml-list] syntax for variable arity? Michael Leary
@ 2001-08-31 7:35 ` Remi VANICAT
0 siblings, 0 replies; 2+ messages in thread
From: Remi VANICAT @ 2001-08-31 7:35 UTC (permalink / raw)
To: caml-list
Michael Leary <leary@nwlink.com> writes:
> I want to write a function to abstract a series of functions with different
> numbers of args like these two:
>
> let debug message = printf "Debug %s\n" message; fso
> let debug_line start_angle start_radius end_angle end_radius =
> printf "DebugLine %f %f %f %f\n" start_angle start_radius end_angle end_radius;
> fso
>
>
> I was thinking of:
>
> type command =
> Debug
> | Debug_line
> | ...
won't work : ocaml can't make a so easily function with different
numbers of args.
>
> let com command _ =
> match command with
> | Debug ->
> doit "some special format string" _
> | Debug_line ->
> doit "other format string" _
>
> let doit _ = printf _ ; fso (* fso defined elsewhere *)
>
>
> let () = com Debug "debug message"
> ...
> let () = com Debug_line start_angle start_radius end_angle end_radius
>
>
> I know _ matches any value(s??), but I'm not clear on how to use the
> match elsewhere... am I close? Is there a better way to structure
> this?
_ match only one value, it's the same to do
let f _ = something
and
let f x = sommething where x is not used
the solution for your problem is something as :
type command
| Debug of string
| Debugline of float * float * float * float
let com command =
begin
match command with
| Debug st -> printf "Debug %s\n" st
| Debugline (start_angle, start_radius, end_angle, end_radius) ->
printf "DebugLine %f %f %f %f\n" start_angle start_radius
end_angle end_radius
end; fso
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2001-08-31 8:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-30 21:45 [Caml-list] syntax for variable arity? Michael Leary
2001-08-31 7:35 ` Remi VANICAT
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox