List Info

Thread: "ocaml_beginners"::[] closure like in Perl, howto?




"ocaml_beginners"::[] closure like in Perl, howto?
country flaguser name
Germany
2007-09-10 09:46:37

Hello,

I am trying to convert a perl script to Ocaml. Is it possible to port a
closure like ff. example to Ocaml? What will be an alternative
expression?

Perl:

{
my $last_max= 0;
sub count_max {
my $list_ref=$_[0];
..
$last_max=$max;
return $max;
}
}
..
foreach my $variant_ref (variant_lists) {
print count_max($variant_ref);
}

I think this is only solevable by using ref values, I am right?

Ocaml:
let last_max_freq = ref 0;;
let count_max = function vlist ->
..
last_max_freq:=max;
in
..

Is there a variant possible without using references?

Bye Andreas

--
Software Developer / Dipl. Inform. (FH)
Max Planck Institute for Human Cognitive and Brain Sciences
Department of Psychology
Stephanstr. 1a, 04103 Leipzig, Germany

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] closure like in Perl, howto?
country flaguser name
United States
2007-09-10 10:21:59

On Mon, 10 Sep 2007 16:46:37 +0200, Andreas Romeyke wrote

> Ocaml:
> let last_max_freq = ref 0;;
> let count_max = function vlist ->
> ..
> last_max_freq:=max;
> in
> ..
>
> Is there a variant possible without using references?

Assuming that my weak Perl skills have deciphered what you want to do here,
the answer is sort of yes, sort of no.

What it looks like to me is that you want to keep some state local to some
function across invocations, so you don't have to explicitly pass the state
on each call, right? The only basic ways to do what you want (warning, I'm
tired and may be forgetting something simple) are via references or objects.

references (cleaner):

let count_max =
let ref last_max_freq = ref 0 in
function vlist ->
...
;;

objects:

class thingy =
object
val mutable last_max_freq = 0
...
method count_max vlist = ...
...
end
;;

Now, if you only need to keep this state while processing batches of stuff,
resetting after each batch, you could do something like fold over your batch
and having the fold pass the state for you:

fold:
let count_max last_max_freq vlist =
...
in
let final max_freq = List.fold left count_max 0 list_of_vlists;;

Or, if you want to get funky with this, you could probably use the pa_monad
extension <http://www.cas.mcmaster.ca/~carette/pa_monad/> to thread the
state for you, but that's probably overload for this situation.

--

William D. Neumann

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] closure like in Perl, howto?
country flaguser name
United Kingdom
2007-09-10 17:23:40

On Mon, Sep 10, 2007 at 09:21:59AM -0600, William D. Neumann wrote:
&gt; On Mon, 10 Sep 2007 16:46:37 +0200, Andreas Romeyke wrote
&gt;
> > Ocaml:
&gt; > let last_max_freq = ref 0;;
> > let count_max = function vlist ->
&gt; > ..
> > last_max_freq:=max;
> > in
> > ..
> >
> > Is there a variant possible without using references?
>
> Assuming that my weak Perl skills have deciphered what you want to do here,
> the answer is sort of yes, sort of no.
>
> What it looks like to me is that you want to keep some state local to some
> function across invocations, so you don't have to explicitly pass the state
> on each call, right? The only basic ways to do what you want (warning, I'm
> tired and may be forgetting something simple) are via references or objects.
>
> references (cleaner):
>
> let count_max =
> let ref last_max_freq = ref 0 in
> function vlist ->
&gt; ...
> ;;

In addition you can use this technique to store state which is shared
by several functions, but invisible to the outside world. Simple
example:

let append, get, clear =
let list = ref [] in
let append elem =
list := elem :: !list
in
let get () =
List.rev !list
in
let clear () =
list := []
in
append, get, clear ;;

Here's how these 3 functions are used:

# get () ;;
- : '_a list = []
# append 1 ;;
- : unit = ()
# append 2 ;;
- : unit = ()
# append 3 ;;
- : unit = ()
# get () ;;
- : int list = [1; 2; 3]
# append 4 ;;
- : unit = ()
# get () ;;
- : int list = [1; 2; 3; 4]
# clear () ;;
- : unit = ()
# append 5 ;;
- : unit = ()
# get () ;;
- : int list = [5]

Note also that the underlying list is hidden from code other than
these 3 functions.

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] closure like in Perl, howto?
country flaguser name
Germany
2007-09-11 01:04:20

Hi,

First, thanks to all...

Am Mon, 10 Sep 2007 23:23:40 +0100
schrieb Richard Jones < rich%40annexia.org">richannexia.org>:

> In addition you can use this technique to store state which is shared
&gt; by several functions, but invisible to the outside world. Simple
&gt; example:
>
> let append, get, clear =
> let list = ref [] in
> let append elem =
> list := elem :: !list
&gt; in
> let get () =
> List.rev !list
&gt; in
> let clear () =
> list := []
> in
> append, get, clear ;;
>
> Here's how these 3 functions are used:

Right, this is the solution stated in my mind....

I was not sure, if the closured variable should be inside of functions
or surrounding them. This aspect was not clearly described in the
manuals I read. But your example is enlighten me.

Thanks...

Bye Andreas

--
Software Developer / Dipl. Inform. (FH)
Max Planck Institute for Human Cognitive and Brain Sciences
Department of Psychology
Stephanstr. 1a, 04103 Leipzig, Germany

[Non-text portions of this message have been removed]

__._,_.___
Recent Activity
Visit Your Group
SPONSORED LINKS
New web site?

Drive traffic now.

Get your business

on Yahoo! search.

Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Yahoo! Groups

Join a yoga group

and take the stress

out of your life.

[1-4]

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