List Info

Thread: Re: "ocaml_beginners"::[] What's difference between both types ?




Re: "ocaml_beginners"::[] What's difference between both types ?
country flaguser name
United States
2007-05-12 02:44:56

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_beginnersyahoogroups.com, "William D. Neumann&quot;
<wneumann...> wrote:
&gt;
>
> 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.
&gt;
> 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:
&gt;
> # third (1,"Jeff",false,2.234);;
> Characters 7-29:
&gt; third (1,"Jeff",false,2.234);;
> ^^^^^^^^^^^^^^^^^^^^^^
> This expression has type int * string * bool * float
&gt; but is here used with type 'a * 'b * 'c
>
>
> William D. Neumann
>
> ---
>

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] What's difference between both types ?
country flaguser name
United Kingdom
2007-05-12 03:26:13

On Saturday 12 May 2007 08:44, LORENZO wrote:
&gt; 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

You can reverse the list as the last step:

# let rec list_init f n list =
if n=0 then List.rev list else
list_init f (n-1) (f() :: list);;
val list_init : (unit -> 'a) -> int -> 'a list -> 'a list = <fun>;

> for user input, I realized it may not to accept
&gt; different functions which produce elements
> of different types.

Your function is polymorphic (the 'a in the type) so it can be used to
generate lists of different types. However, lists are homogeneous in OCaml,
so all the elements of a list are of the same type.

If you want to bring several types together into one new type then you must
use a sum type, like this:

# type number = Int of int | Float of float;;
type number = Int of int | Float of float

Now you can create lists that contain ints and floats:

# [Int 3; Float 5.4];;
- : number list = [Int 3; Float 5.4]

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )