| Newsgroups |
gmane.comp.lang.ocaml.beginners |
| Message-ID |
<[email protected]> |
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
>
>
>