My deeply appreciation for your
descriptions of list and tuple in detail.
I spent some time trying to make following
function to be more generalized, such that
previous question about tuple origined from...
# let putinlst f lim =
let rec put_lst f lim acc =
if lim <=0 then acc
else put_lst f (lim-1) ( f () :: acc) in
put_lst f lim [] ;;
val putinlst : (unit -> 'a) -> int -> 'a list = <fun>
Besides it always construct a list backwards
for user input, I realized it may not to accept
different functions which produce elements
of different types.
--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com, "William D. Neumann"
<wneumann
...> wrote:
>
>
> However, lists and arrays both have the restriction that all of
their elements have to de of
> the same type, so we can't hove, for example, [1.141; "Snoopy"], as
this
> tries to mix a float and a string in a list.
>
> If we want to mix values, we then need to use either records,
tuples, or
> variants -- all of which are related.
>
>
> Another difference here is that these functions will work on *any*
three
> element tuple (as long as you don't restrict the types in your
> definition). So we can do:
> name (3.1415,'z',["slappy"; "puppy"; "sleepy"]);;
> age (3.1415,'z',["slappy"; "puppy"; "sleepy"]);;
> married (3.1415,'z',["slappy"; "puppy"; "sleepy"]);;
>
> And get back 3.1415 from the first call, 'z' from the second, and
> ["slappy"; "puppy"; "sleepy"] from the third. This is, again, because
> they work on position alone. We could have named the functions first,
> second, and third instead.
>
> elements. If we try to pass it a tuple with four elements, we got an
> error:
>
> # third (1,"Jeff",false,2.234);;
> Characters 7-29:
> third (1,"Jeff",false,2.234);;
> ^^^^^^^^^^^^^^^^^^^^^^
> This expression has type int * string * bool * float
> but is here used with type 'a * 'b * 'c
>
>
> William D. Neumann
>
> ---
>
.