Force pre-computation of a constant

前端 未结 1 1594
说谎
说谎 2021-02-04 08:57

I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I\'m seeing some code that looks roughly like,

myList = [(a, b), (c,          


        
相关标签:
1条回答
  • 2021-02-04 09:33

    Generating a compile-time constant using Template Haskell:

    {-# LANGUAGE TemplateHaskell #-}
    import Language.Haskell.TH.Syntax(Lift(..))
    
    test = $(lift $ (map (*20) [0..100] :: [Int]))
    

    lift takes a Haskell value and lifts it into a TH Exp. The $() runs the enclosed quote, and splices the generated exp into the code at compile time.

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