Mutating state is at the center of the builder pattern. Is there an idiomatic way to implement the internals of such a class in F# that will reduce/eliminate mutable state while retaining the usual interface (this class will be used mostly from other .NET languages)?

Here's a naive implementation:

```
type QueryBuilder<'T>() =                              //'
    let where = ref None
    let orderBy = ref None
    let groupBy = ref None
    member x.Where(cond) =
        match !where with
        | None -> where := Some(cond)
        | _ -> invalidOp "Multiple WHERE clauses are not permitted"
    // members OrderBy and GroupBy implemented similarly

```

One idea is to create a record type to store the internals, and use *copy and update* expressions.

```
type private QueryBuilderSpec<'T> =                     //'
    { Where : ('T -> bool) option;                      //'
      OrderBy : (('T -> obj) * bool) list;              //'
      GroupBy : ('T -> obj) list }                      //'

type QueryBuilder<'T>() =                               //'
    let spec = ref None
    member x.Where(cond) =
        match !spec with
        | None -> 
            spec := Some({ Where = Some(cond); OrderBy = []; GroupBy = [] })
        | Some({ Where = None; OrderBy = _; GroupBy = _} as s) -> 
            spec := Some({ s with Where = Some(cond) })
        | _ -> invalidOp "Multiple WHERE clauses are not permitted"
    // members OrderBy and GroupBy implemented similarly

```

This all seems a bit clunky, and maybe that should be expected when trying to implement an imperative pattern in F#. Is there a better way to do this, again, retaining the usual builder interface for the sake of imperative languages?