* Curried form different from "normal"
@ 2006-02-05 16:40 Till Varoquaux
2006-02-05 16:59 ` [Caml-list] " David Baelde
2006-02-05 18:00 ` Jon Harrop
0 siblings, 2 replies; 4+ messages in thread
From: Till Varoquaux @ 2006-02-05 16:40 UTC (permalink / raw)
To: caml-list
I can across something that puzzled me while develloping a caml application:
Using:
let non_blank_string=
if !is_first then
trailling:=Buffer.contents white
else
Buffer.add_buffer buf white;
Buffer.reset white;
is_first:=false;
Buffer.add_string buf
instead of
let non_blank_string s=
if !is_first then
trailling:=Buffer.contents white
else
Buffer.add_buffer buf white;
Buffer.reset white;
is_first:=false;
Buffer.add_string buf s
Doesn't yeld the same results (the former is buggy). Now, as I
understand, these two functions should be exactly equivalent. I am
misunderstanding something here?
Cheers,
Till Varoquaux
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Curried form different from "normal"
2006-02-05 16:40 Curried form different from "normal" Till Varoquaux
@ 2006-02-05 16:59 ` David Baelde
2006-02-05 18:00 ` Jon Harrop
1 sibling, 0 replies; 4+ messages in thread
From: David Baelde @ 2006-02-05 16:59 UTC (permalink / raw)
To: Till Varoquaux; +Cc: caml-list
Hi,
Your statement is true only for purely functional definitions. Here,
there is a difference. The following piece of code is evaluated only
one, at the time of the outermost definition. So trailing is
instantiated only once.
> if !is_first then
> trailling:=Buffer.contents white
> else
> Buffer.add_buffer buf white;
> Buffer.reset white;
> is_first:=false;
This kind of effect is sometimes expected, however.
Have fun.
--
David
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Curried form different from "normal"
2006-02-05 16:40 Curried form different from "normal" Till Varoquaux
2006-02-05 16:59 ` [Caml-list] " David Baelde
@ 2006-02-05 18:00 ` Jon Harrop
2006-02-05 18:14 ` Till Varoquaux
1 sibling, 1 reply; 4+ messages in thread
From: Jon Harrop @ 2006-02-05 18:00 UTC (permalink / raw)
To: caml-list
On Sunday 05 February 2006 16:40, Till Varoquaux wrote:
> let non_blank_string=
> if !is_first then
> trailling:=Buffer.contents white
> else
> Buffer.add_buffer buf white;
> Buffer.reset white;
> is_first:=false;
> Buffer.add_string buf
>
> let non_blank_string s=
> if !is_first then
> trailling:=Buffer.contents white
> else
> Buffer.add_buffer buf white;
> Buffer.reset white;
> is_first:=false;
> Buffer.add_string buf s
>
> Doesn't yeld the same results (the former is buggy). Now, as I
> understand, these two functions should be exactly equivalent. I am
> misunderstanding something here?
The code preceding the call to "Buffer.add_string" is only evaluated once in
the former case (at the time of definition) whereas it is evaluated at each
invocation of the latter definition of "non_blank_string".
You should not have expected these two definitions to be "exactly equivalent"
as the former is equivalent to:
if !is_first then
trailling:=Buffer.contents white
else
Buffer.add_buffer buf white;;
Buffer.reset white;;
is_first:=false;;
let non_blank_string =
Buffer.add_string buf
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Curried form different from "normal"
2006-02-05 18:00 ` Jon Harrop
@ 2006-02-05 18:14 ` Till Varoquaux
0 siblings, 0 replies; 4+ messages in thread
From: Till Varoquaux @ 2006-02-05 18:14 UTC (permalink / raw)
To: caml-list
Yes, I apologize for bugging all of you. This makes sense and I could
(should) have figured it out by myself.
As david pointed out the equivalence is only true for purely
functional code. I guess this is one of the consequences of flirting
to much with imperative features...
Till Varoquaux
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-02-05 18:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-05 16:40 Curried form different from "normal" Till Varoquaux
2006-02-05 16:59 ` [Caml-list] " David Baelde
2006-02-05 18:00 ` Jon Harrop
2006-02-05 18:14 ` Till Varoquaux
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox