Compile-time calculation: Difference between revisions

→‎{{header|Haskell}}: previous code was not doing compile-time calculation
(→‎{{header|Haskell}}: previous code was not doing compile-time calculation)
Line 328:
=={{header|Haskell}}==
 
With Template Haskell and Quasiquotes, it is quite easy to do compile time embedding. The functions used at compile-time need to be already compiled. Therefore, you generally need two modules.
 
<lang haskell>{-#module LANGUAGEFactorial QuasiQuotes, TemplateHaskell #-}where
import Language.Haskell.TH.Syntax
 
fact n = product [1..n]
 
factQ :: Integer -> Q Exp
main = print $([|fact 10|])</lang>
factQ = lift . fact</lang>
 
<lang haskell>{-# LANGUAGE TemplateHaskell #-}
import Factorial
 
main = print $([|factfactQ 10|])</lang>
 
Note: Doing <code>$([|fact 10|])</code> is the same than doing <code>fact 10</code>. <code>[|something|]</code> returns the abstract syntax tree of <code>something</code>. Thus <code>[|fact 10|]</code> returns the AST of the call to the <code>fact</code> function with 10 as argument. <code>$(something)</code> waits for an AST from a call to <code>something</code>.
 
=={{header|J}}==
Anonymous user