List Info

Thread: Re: "ocaml_beginners"::[] Side Effect With Iterate




Re: "ocaml_beginners"::[] Side Effect With Iterate
country flaguser name
United States
2007-07-30 05:05:16

Thanks to your suggestions, I then tried
imperative way to produce the required result,

# let newsymb = let c = ref 0 in
function s -> c := !c +1; s^(string_of_int !c) ;;

# let _ = for i =0 to 3 do
print_endline (newsymb "Var"); done ;;

Besides I was confused with different types of below:

# let newsymb1 = let c = ref 0 in
function s -> s^(string_of_int !c); c := !c +1 ;;
Characters 47-67:
Warning S: this expression should have type unit.
function s -> s^(string_of_int !c); c := !c +1 ;;
^^^^^^^^^^^^^^^^^^^^
val newsymb1 : string -> unit = <fun>;

# let newsymb2 = let c = ref 0 in
function s -> c := !c +1; s^(string_of_int !c) ;;
val newsymb2 : string -> string = <fun>;

Why the order of statement c := !c +1 changed,
the types of two function changed ?

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Fabrice Marchant
<fabrice.marchant...> wrote:
&gt;
> Hi Lorenzo,
>
> I do not think there is a side effect problem but rather a
conception issue.
&gt;
> Such an iterating function - that make remember the "fold"s of the
library - can be useful if the "iterated" function can easily continue
its work leaning on the result of the nested call.
&gt;
> If you absolutely want the functional "iterate" on "new-symb"
behaves like this :
>
>
> It would be so simple to stringify the number argument (3) and to
concatenate to "Var&quot;...
>
> Regards,
>
> Fabrice
>

__._,_.___
Recent Activity
Visit Your Group
SPONSORED LINKS
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Yoga Groups

Find Enlightenment

&amp; exchange insights

with other members

Re: "ocaml_beginners"::[] Side Effect With Iterate
country flaguser name
United Kingdom
2007-07-30 05:33:10

On Monday 30 July 2007 11:05:16 LORENZO wrote:
&gt; Thanks to your suggestions, I then tried
&gt; imperative way to produce the required result,
>
> # let newsymb = let c = ref 0 in
> function s -> c := !c +1; s^(string_of_int !c) ;;
>
> # let _ = for i =0 to 3 do
> print_endline (newsymb "Var&quot;); done ;;
>
> Besides I was confused with different types of below:
&gt;
> # let newsymb1 = let c = ref 0 in
> function s -> s^(string_of_int !c); c := !c +1 ;;
> Characters 47-67:
&gt; Warning S: this expression should have type unit.
&gt; function s -> s^(string_of_int !c); c := !c +1 ;;
> ^^^^^^^^^^^^^^^^^^^^
> val newsymb1 : string -> unit = <fun>;
>
> # let newsymb2 = let c = ref 0 in
> function s -> c := !c +1; s^(string_of_int !c) ;;
> val newsymb2 : string -> string = <fun>;
>
> Why the order of statement c := !c +1 changed,
> the types of two function changed ?

If you write two expressions:

f; g

then "f&quot; must evaluate to the value () of the type unit and the result of "g&quot;
will be returned.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e

__._,_.___
.

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

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