type FoldStep<'State, 'T> = ('State -> 'T -> 'State) -> 'State -> 'State

type FoldBuilder() =
    member this.Zero(): FoldStep<'State, 'T> = 
        fun _ state -> state
    
    member inline this.Combine(
        [<InlineIfLambda>] f: FoldStep<'State, 'T>, 
        [<InlineIfLambda>] g: FoldStep<'State, 'T>)
        : FoldStep<'State, 'T> =
        fun folder state -> g folder (f folder state)
    
    member inline this.Delay([<InlineIfLambda>] f: unit -> FoldStep<'State, 'T>) = 
        f()
    
    member inline this.Yield(value: 'T): FoldStep<'State, 'T> = 
        fun folder state -> folder state value
    
let fold = FoldBuilder()

let f = (fold { 1; 2; 3; 4; 5 }) (+) 0
printfn "%d" f