Re: Overloaded functions in Ocaml?

"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 2 Mar 2016 23:17:34 -0600
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAM0XMJS4DwEWpRHeiCLnP5h6H7t1WORF4k05uryZi5BLZz4VQw@mail.gmail.com>
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
>
>
>
>
> 
>