Re: Overloaded functions in Ocaml?
"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Wed, 2 Mar 2016 15:49:34 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
No, there's not really overloaded functions. You can use either general polymorphy or a specific set of admissible types in the signature. Then just use it where-ever for whatever you want. There's not so much a need to explicitly alter for a given type so long as each of the functions within are defined all the way down for a polymorphic instance. In the case of floats vs ints however, the answer is no because ocaml specifically distinguishes the primitive operations that are admissible on the foundational types (+ vs +.) In any case, you could easily use the module system for that purpose by designing all your code within a functor and agnosticizing the code of the particular type of the primitive you think you need overloading for. Then just implement the function in a module specific to the type and pass the module as an argument to the functor to derive an applicable final resultant module.
Separating the integer and float operations with the specific designations is necessary for hindley milner type inference. If you notice, the type checker alleviates the pain of needing to specify a type every time by figuring out what you meant. Types flow from within function definitions outward, starting from the inside, in working with the base types. So if you define a function, the type checker looks at the functions and type manipulation requirements within that function to know what the signature and return type are. Not distinguishing float and int, are therefore an ambiguity to the type checker.
On Wednesday, March 2, 2016 10:41 AM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
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
#yiv3920645058 #yiv3920645058 -- #yiv3920645058ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv3920645058 #yiv3920645058ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv3920645058 #yiv3920645058ygrp-mkp #yiv3920645058hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv3920645058 #yiv3920645058ygrp-mkp #yiv3920645058ads {margin-bottom:10px;}#yiv3920645058 #yiv3920645058ygrp-mkp .yiv3920645058ad {padding:0 0;}#yiv3920645058 #yiv3920645058ygrp-mkp .yiv3920645058ad p {margin:0;}#yiv3920645058 #yiv3920645058ygrp-mkp .yiv3920645058ad a {color:#0000ff;text-decoration:none;}#yiv3920645058 #yiv3920645058ygrp-sponsor #yiv3920645058ygrp-lc {font-family:Arial;}#yiv3920645058 #yiv3920645058ygrp-sponsor #yiv3920645058ygrp-lc #yiv3920645058hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv3920645058 #yiv3920645058ygrp-sponsor #yiv3920645058ygrp-lc .yiv3920645058ad {margin-bottom:10px;padding:0 0;}#yiv3920645058 #yiv3920645058actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv3920645058 #yiv3920645058activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv3920645058 #yiv3920645058activity span {font-weight:700;}#yiv3920645058 #yiv3920645058activity span:first-child {text-transform:uppercase;}#yiv3920645058 #yiv3920645058activity span a {color:#5085b6;text-decoration:none;}#yiv3920645058 #yiv3920645058activity span span {color:#ff7900;}#yiv3920645058 #yiv3920645058activity span .yiv3920645058underline {text-decoration:underline;}#yiv3920645058 .yiv3920645058attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv3920645058 .yiv3920645058attach div a {text-decoration:none;}#yiv3920645058 .yiv3920645058attach img {border:none;padding-right:5px;}#yiv3920645058 .yiv3920645058attach label {display:block;margin-bottom:5px;}#yiv3920645058 .yiv3920645058attach label a {text-decoration:none;}#yiv3920645058 blockquote {margin:0 0 0 4px;}#yiv3920645058 .yiv3920645058bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv3920645058 .yiv3920645058bold a {text-decoration:none;}#yiv3920645058 dd.yiv3920645058last p a {font-family:Verdana;font-weight:700;}#yiv3920645058 dd.yiv3920645058last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv3920645058 dd.yiv3920645058last p span.yiv3920645058yshortcuts {margin-right:0;}#yiv3920645058 div.yiv3920645058attach-table div div a {text-decoration:none;}#yiv3920645058 div.yiv3920645058attach-table {width:400px;}#yiv3920645058 div.yiv3920645058file-title a, #yiv3920645058 div.yiv3920645058file-title a:active, #yiv3920645058 div.yiv3920645058file-title a:hover, #yiv3920645058 div.yiv3920645058file-title a:visited {text-decoration:none;}#yiv3920645058 div.yiv3920645058photo-title a, #yiv3920645058 div.yiv3920645058photo-title a:active, #yiv3920645058 div.yiv3920645058photo-title a:hover, #yiv3920645058 div.yiv3920645058photo-title a:visited {text-decoration:none;}#yiv3920645058 div#yiv3920645058ygrp-mlmsg #yiv3920645058ygrp-msg p a span.yiv3920645058yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv3920645058 .yiv3920645058green {color:#628c2a;}#yiv3920645058 .yiv3920645058MsoNormal {margin:0 0 0 0;}#yiv3920645058 o {font-size:0;}#yiv3920645058 #yiv3920645058photos div {float:left;width:72px;}#yiv3920645058 #yiv3920645058photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv3920645058 #yiv3920645058photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv3920645058 #yiv3920645058reco-category {font-size:77%;}#yiv3920645058 #yiv3920645058reco-desc {font-size:77%;}#yiv3920645058 .yiv3920645058replbq {margin:4px;}#yiv3920645058 #yiv3920645058ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv3920645058 #yiv3920645058ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv3920645058 #yiv3920645058ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv3920645058 #yiv3920645058ygrp-mlmsg select, #yiv3920645058 input, #yiv3920645058 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv3920645058 #yiv3920645058ygrp-mlmsg pre, #yiv3920645058 code {font:115% monospace;}#yiv3920645058 #yiv3920645058ygrp-mlmsg * {line-height:1.22em;}#yiv3920645058 #yiv3920645058ygrp-mlmsg #yiv3920645058logo {padding-bottom:10px;}#yiv3920645058 #yiv3920645058ygrp-msg p a {font-family:Verdana;}#yiv3920645058 #yiv3920645058ygrp-msg p#yiv3920645058attach-count span {color:#1E66AE;font-weight:700;}#yiv3920645058 #yiv3920645058ygrp-reco #yiv3920645058reco-head {color:#ff7900;font-weight:700;}#yiv3920645058 #yiv3920645058ygrp-reco {margin-bottom:20px;padding:0px;}#yiv3920645058 #yiv3920645058ygrp-sponsor #yiv3920645058ov li a {font-size:130%;text-decoration:none;}#yiv3920645058 #yiv3920645058ygrp-sponsor #yiv3920645058ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv3920645058 #yiv3920645058ygrp-sponsor #yiv3920645058ov ul {margin:0;padding:0 0 0 8px;}#yiv3920645058 #yiv3920645058ygrp-text {font-family:Georgia;}#yiv3920645058 #yiv3920645058ygrp-text p {margin:0 0 1em 0;}#yiv3920645058 #yiv3920645058ygrp-text tt {font-size:120%;}#yiv3920645058 #yiv3920645058ygrp-vital ul li:last-child {border-right:none !important;}#yiv3920645058