let sprite = pipe4 sprite_start blanks (manyTill attribute newline) blanks (fun a b c _ -> Sprite(a,c))
let sprites =
let rec y f x = f (y f) x // The Y Combinator
//let rec sprites_up = many1Indents sprite sprites_up |>> (fun x -> SubSprites x) // Does not work.
let sprites_up = y (fun f -> many1Indents sprite f |>> (fun x -> SubSprites x))
many1Indents sprite sprites_up
Here is an example from the compiler for a smallish game library I am making in F#. More specifically, in the above I need to have the sprites_up recursively call itself otherwise the indentation parser would not work correctly.
Without the Y Combinator, I could not have done the parser properly and would have had to resort to writing something like this:
let sprites_up4 = many1Indents sprite error |>> (fun x -> SubSprites x)
let sprites_up3 = many1Indents sprite sprites_up4 |>> (fun x -> SubSprites x)
let sprites_up2 = many1Indents sprite sprites_up3 |>> (fun x -> SubSprites x)
let sprites_up1 = many1Indents sprite sprites_up2 |>> (fun x -> SubSprites x)
let sprites_up = many1Indents sprite sprites_up1 |>> (fun x -> SubSprites x)
Would not have been a great solution, the Y Combinator really saved me there. It was certainly not the first thing that came to mind though.