Haskell GHC: what is the time complexity of a pattern match with N constructors?

后端 未结 1 454
长发绾君心
长发绾君心 2020-11-30 04:38

Let\'s say we have the following Haskell:

data T = T0 | T1 | T2 | ... | TN

toInt :: T -> Int
toInt t = case t of
  T0 -> 0
  T1 -> 1
  T2 -> 2
          


        
相关标签:
1条回答
  • 2020-11-30 05:28

    A jump table is used, making the pattern-match a constant time operation.

    Unfortunately I'm unable to find an up-to-date citation for this, although this page mentions the implementation of Cmm-level switch statements as jump tables, and this old tagging design document uses a case on a Bool as an example, producing a jump table.

    0 讨论(0)
提交回复
热议问题