--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com, "Lukasz Stafiniak" <lukstafi
...> wrote:
> 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) ->
> (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) ->
> (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
> | `Var v -> None;;
>
> This way only substitution paths are copied.
Yes. So in fact, this is not really an example where "short, recursive coding"
is inefficient : the point is just that you have to do some extra thinking before writing out
the code.
Ewan
.