Re: Overloaded functions in Ocaml?
"Sergei Steshenko [email protected] [ocaml_beginners]" <[email protected]> Thu, 3 Mar 2016 15:44:09 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
"BUT.... we need floats! I can't imagine studying any technical discipline using only ints. ( Except maybe discrete math. )" - it's a somewhat funny statement.
When digital computer calculates (in this list I should probably better say evaluates), it performs discrete math operations.Floating point numbers in digital computer are very far from real (in Russian a subset of real numbers is called "irrational numbers" for quite a good reason) numbers in non-discrete math.
One of the most vivid examples is sum of
1/n
series ( Harmonic series (mathematics) - Wikipedia, the free encyclopedia ). https://en.wikipedia.org/wiki/Harmonic_series_%28mathematics%29
| |
| | | | | | | |
| Harmonic series (mathematics) - Wikipedia, the free ency...In mathematics, the harmonic series is the divergent infinite series: |
| |
| View on en.wikipedia.org | Preview by Yahoo |
| |
| |
In real non-discrete math the sum is infinity.
In a digital computer the sum will be finite due to finite number of bits and machine epsilon.
Floating point numbers in digital computer are for convenience. I.e. the programmer is relieved of the duty to deal with scaling fourth and back - the FPU does the job. But other than that floating point numbers are just awkward integers.
One of the gravest mistakes in modern education is teaching programming before numerical methods of computations - if the latter are at all taught. Luckily, I was taught in the opposite order.
--Sergei.
From: "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]>
To: [email protected]
Sent: Thursday, March 3, 2016 7:17 AM
Subject: Re: "ocaml_beginners"::[] Overloaded functions in Ocaml?
Thanks everyone for these great answers. I didn't finish reading the recommended article, but the beginning of it looked really interesting. I'm not working with user-defined types in Ocaml yet, but.... that's my next step for sure. But I have done a lot of work with arrays, 2D-arrays, and I love using recursive functions to manipulate lists. Very cool. Although I don't understand why lists are called immutable in Ocaml or any functional language. If a list is really an immutable or "frozen" object ( borrowing a term from Ruby ) then it should be impossible to do this:
1 :: [2; 3; 4; 5] ;;
Unless of course [1; 2; 3; 4; 5] and [2; 3; 4; 5] are two separate lists with different memory addresses. ( And the old list gets garbage collected since a variable name no longer references it? )
Sorry if these questions are a little elementary, but I'm still in the learning stages.
Oh yeah, parametric polymorphism looks especially interesting. I'll have to explore that much further. I like the List.length example. That function is not limited to lists of any one type, but works on lists of all types. I like that example!
Regarding ints and floats, I do recall a linear algebra professor talking about how computers are usually pretty good at representing floats near 0. As you move farther and farther out from 0, the distance between adjacent floats actually increases. I think this is really more of a machine epsilon issue than a pure math issue. BUT.... we need floats! I can't imagine studying any technical discipline using only ints. ( Except maybe discrete math. )
On Wed, Mar 2, 2016 at 12:12 PM, 'Mr. Herr' [email protected] [ocaml_beginners] <[email protected]> wrote:
see my answers below
/Str.
On 02.03.2016 16:41, Douglas Lewit [email protected] [ocaml_beginners] 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:
"overloaded" functions means function takes arguments of different types and even different arity. This is precisely what OCaml does not have.
There is however "parametric polymorphism", functions take arguments of one type, and some of any type.
Example: List.length has the signature 'a list -> int. This which means a list of type 'a, and this is polymorphic.
Example 2 in toplevel, create a type consisting of int and string, and use it:
:
# type intsandstrings = Astring of string | Anint of int ;;
type intsandstrings = Astring of string | Anint of int
# let f x = match x with
Astring s -> Printf.printf "got string %s\n" s
| Anint i -> Printf.printf "got binary int value %i\n" i
;;
val f : intsandstrings -> unit = <fun>
# f (Astring "hi");;
got string hi
- : unit = ()
# f (Anint 99);;
got binary int value 99
- : unit = ()
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?
If you can provide a function for "next element", you can do this.
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?
see http://stackoverflow.com/questions/33413163
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
#yiv6113214512 #yiv6113214512 -- #yiv6113214512ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv6113214512 #yiv6113214512ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv6113214512 #yiv6113214512ygrp-mkp #yiv6113214512hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv6113214512 #yiv6113214512ygrp-mkp #yiv6113214512ads {margin-bottom:10px;}#yiv6113214512 #yiv6113214512ygrp-mkp .yiv6113214512ad {padding:0 0;}#yiv6113214512 #yiv6113214512ygrp-mkp .yiv6113214512ad p {margin:0;}#yiv6113214512 #yiv6113214512ygrp-mkp .yiv6113214512ad a {color:#0000ff;text-decoration:none;}#yiv6113214512 #yiv6113214512ygrp-sponsor #yiv6113214512ygrp-lc {font-family:Arial;}#yiv6113214512 #yiv6113214512ygrp-sponsor #yiv6113214512ygrp-lc #yiv6113214512hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv6113214512 #yiv6113214512ygrp-sponsor #yiv6113214512ygrp-lc .yiv6113214512ad {margin-bottom:10px;padding:0 0;}#yiv6113214512 #yiv6113214512actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv6113214512 #yiv6113214512activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv6113214512 #yiv6113214512activity span {font-weight:700;}#yiv6113214512 #yiv6113214512activity span:first-child {text-transform:uppercase;}#yiv6113214512 #yiv6113214512activity span a {color:#5085b6;text-decoration:none;}#yiv6113214512 #yiv6113214512activity span span {color:#ff7900;}#yiv6113214512 #yiv6113214512activity span .yiv6113214512underline {text-decoration:underline;}#yiv6113214512 .yiv6113214512attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv6113214512 .yiv6113214512attach div a {text-decoration:none;}#yiv6113214512 .yiv6113214512attach img {border:none;padding-right:5px;}#yiv6113214512 .yiv6113214512attach label {display:block;margin-bottom:5px;}#yiv6113214512 .yiv6113214512attach label a {text-decoration:none;}#yiv6113214512 blockquote {margin:0 0 0 4px;}#yiv6113214512 .yiv6113214512bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv6113214512 .yiv6113214512bold a {text-decoration:none;}#yiv6113214512 dd.yiv6113214512last p a {font-family:Verdana;font-weight:700;}#yiv6113214512 dd.yiv6113214512last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv6113214512 dd.yiv6113214512last p span.yiv6113214512yshortcuts {margin-right:0;}#yiv6113214512 div.yiv6113214512attach-table div div a {text-decoration:none;}#yiv6113214512 div.yiv6113214512attach-table {width:400px;}#yiv6113214512 div.yiv6113214512file-title a, #yiv6113214512 div.yiv6113214512file-title a:active, #yiv6113214512 div.yiv6113214512file-title a:hover, #yiv6113214512 div.yiv6113214512file-title a:visited {text-decoration:none;}#yiv6113214512 div.yiv6113214512photo-title a, #yiv6113214512 div.yiv6113214512photo-title a:active, #yiv6113214512 div.yiv6113214512photo-title a:hover, #yiv6113214512 div.yiv6113214512photo-title a:visited {text-decoration:none;}#yiv6113214512 div#yiv6113214512ygrp-mlmsg #yiv6113214512ygrp-msg p a span.yiv6113214512yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv6113214512 .yiv6113214512green {color:#628c2a;}#yiv6113214512 .yiv6113214512MsoNormal {margin:0 0 0 0;}#yiv6113214512 o {font-size:0;}#yiv6113214512 #yiv6113214512photos div {float:left;width:72px;}#yiv6113214512 #yiv6113214512photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv6113214512 #yiv6113214512photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv6113214512 #yiv6113214512reco-category {font-size:77%;}#yiv6113214512 #yiv6113214512reco-desc {font-size:77%;}#yiv6113214512 .yiv6113214512replbq {margin:4px;}#yiv6113214512 #yiv6113214512ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv6113214512 #yiv6113214512ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv6113214512 #yiv6113214512ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv6113214512 #yiv6113214512ygrp-mlmsg select, #yiv6113214512 input, #yiv6113214512 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv6113214512 #yiv6113214512ygrp-mlmsg pre, #yiv6113214512 code {font:115% monospace;}#yiv6113214512 #yiv6113214512ygrp-mlmsg * {line-height:1.22em;}#yiv6113214512 #yiv6113214512ygrp-mlmsg #yiv6113214512logo {padding-bottom:10px;}#yiv6113214512 #yiv6113214512ygrp-msg p a {font-family:Verdana;}#yiv6113214512 #yiv6113214512ygrp-msg p#yiv6113214512attach-count span {color:#1E66AE;font-weight:700;}#yiv6113214512 #yiv6113214512ygrp-reco #yiv6113214512reco-head {color:#ff7900;font-weight:700;}#yiv6113214512 #yiv6113214512ygrp-reco {margin-bottom:20px;padding:0px;}#yiv6113214512 #yiv6113214512ygrp-sponsor #yiv6113214512ov li a {font-size:130%;text-decoration:none;}#yiv6113214512 #yiv6113214512ygrp-sponsor #yiv6113214512ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv6113214512 #yiv6113214512ygrp-sponsor #yiv6113214512ov ul {margin:0;padding:0 0 0 8px;}#yiv6113214512 #yiv6113214512ygrp-text {font-family:Georgia;}#yiv6113214512 #yiv6113214512ygrp-text p {margin:0 0 1em 0;}#yiv6113214512 #yiv6113214512ygrp-text tt {font-size:120%;}#yiv6113214512 #yiv6113214512ygrp-vital ul li:last-child {border-right:none !important;}#yiv6113214512