Skip to content

[<TailCall>] not used in suggested version example #46257

Open
@JasonKleban

Description

@JasonKleban

Type of issue

Typo

Description

This is given as the Tail Call Optimized version of fib but it doesn't actually use the TailCallAttribute. Furthermore, it is unobvious to me if [<TailCall>] should be placed on fib or on the inner recursive loop binding.

I don't think the attribute can even be applied on loop binding, but fib isn't actually recursive. If putting the attribute on fib is correct, that suggests that any function inside fib would have this TailCall constraint which seems like an inappropriate restriction.

let fib n =
    let rec loop acc1 acc2 n =
        match n with
        | 0 -> acc1
        | 1 -> acc2
        | _ ->
            loop acc2 (acc1 + acc2) (n - 1)
    loop 0 1 n

Can you please clarify in this example where to use [<TailCall>]? And discuss any interesting side-topics about that placement regarding propagation of the constraint?

Page URL

https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/recursive-functions-the-rec-keyword

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fsharp/language-reference/functions/recursive-functions-the-rec-keyword.md

Document Version Independent Id

4aececf7-7876-3bc1-f722-1004b25fe05d

Platform Id

e195630b-53b2-2298-0270-c4ba6e3a3bd9

Article author

@KathleenDollard

Metadata

  • ID: 029d87c5-6502-8151-c903-dec53a570b0a
  • PlatformId: e195630b-53b2-2298-0270-c4ba6e3a3bd9
  • Service: dotnet-fsharp

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions