Overloaded functions in Ocaml?
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 2 Mar 2016 09:41:47 -0600
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJRzLnUkT+ti6b4RGjOimDTWrZUN0JYAMCk9xYqznqCjYA@mail.gmail.com> |
Hi everyone, Quick question about functions in Ocaml. Are there any "overloaded" functions in Ocaml? For example, let's say I have the following function defined in utop: let rec sequence lower upper step = if lower > upper then [ ] else lower :: ( sequence ( lower *+* step ) upper step ) ;; A nice little function that creates a list of sequential integers. Or.... let rec sequence lower upper step = if lower > upper then [ ] else lower :: ( sequence ( lower *+.* step ) upper step ) ;; Almost the same function, but this time the function creates a list of floats. 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. I am just a beginner in Ocaml, so please.... if the answer is very technical pretend you're explaining it to a freshman or sophomore. And thanks for the help! Ocaml has really opened up my eyes to the power of recursion, which has made me a much stronger programmer in other languages. 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. Best, Douglas Lewit