Re: Overloaded functions in Ocaml?
"Christopher Zimmermann [email protected] [ocaml_beginners]" <[email protected]> Wed, 2 Mar 2016 18:13:29 +0100
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
On Wed, 2 Mar 2016 09:41:47 -0600 "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote: > Hi everyone, > > Quick question about functions in Ocaml. Are there any "overloaded" > functions in Ocaml? No, there aren't. > So I guess my question is this. Is there a way to create a more > generic Ocaml function that could generate a list of integers OR > generate a list of floats depending on the arguments that I provide? > There probably is I'm guessing, but I have no clue how to approach > that. Yes, you could either write a stub in C to do the job or use the Obj module (you'll need Obj.is_block Obj.tag and Obj.double_tag). But I wouldn't recomend implementing such a function. Its type would get inferred to 'a -> 'b -> 'c, maybe you could constrain it to 'a -> 'a -> 'a. > One more quick question. Why did Ocaml's developers make it a point > to separate integer computations from floating point computations? I > think I have an idea, but not really sure. I know that computers can > represent integers exactly, but when it comes to floats ( or doubles > in other languages ) computers have to convert between base-2 > fractions and base-10 fractions, and the conversion never yields a > value that is 100% accurate. But most languages freely go back and > forth between ints and floats without worrying too much about any > major loss in precision. Why did Ocaml's developers decide to > completely separate integer computations from floating-point value > computations? It's a very distinctive feature of the language that I > have not found yet in other computer languages that I have > experimented with. Floats and integers are two very different types of data and implicitely converting one to another can have many surprising side effects. At least in my experience I only rarely need float_of_int or int_of_float. Christopher -- http://gmerlin.de OpenPGP: http://gmerlin.de/christopher.pub 2779 7F73 44FD 0736 B67A C410 69EC 7922 34B4 2566