List Info

Thread: Re: "ocaml_beginners"::[] Is ocaml smart on memory usage here?




Re: "ocaml_beginners"::[] Is ocaml smart on memory usage here?
country flaguser name
United States
2007-02-15 08:51:23

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, "Lukasz Stafiniak&quot; <lukstafi...> wrote:
&gt; To expand on this, you need to communicate whether a substitution was
> made, for example with a variant:
>
> let rec subst var value = function
> | `Add(f, g) ->
&gt; (match subst var value f, subst var value g with
>; None, None -> None
>; | Some a1, None -> Some (`Add (a1, g))
> | None, Some a2 -> Some (`Add (f, a2))
>; | Some a1, Some a2 -> Some (`Add (a1, a2))
>; )
> | `Mult(f, g) ->
&gt; (match subst var value f, subst var value g with
>; None, None -> None
>; | Some a1, None -> Some (`Mult (a1, g))
> | None, Some a2 -> Some (`Mult (f, a2))
>; | Some a1, Some a2 -> Some (`Mult (a1, a2))
>; )
> | `Int n -> None
>; | `Var v when v=var -> Some value
&gt; | `Var v -> None;;
&gt;
> This way only substitution paths are copied.

Yes. So in fact, this is not really an example where "short, recursive coding&quot;
is inefficient : the point is just that you have to do some extra thinking before writing out
the code.

Ewan


__._,_.___
.

__,_._,___
[1]

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