Compile-time calculation: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: previous code was not doing compile-time calculation) |
|||
Line 328: | Line 328: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
With Template Haskell |
With Template Haskell, 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> |
<lang haskell>module Factorial where |
||
import Language.Haskell.TH.Syntax |
|||
fact n = product [1..n] |
fact n = product [1..n] |
||
factQ :: Integer -> Q Exp |
|||
⚫ | |||
factQ = lift . fact</lang> |
|||
<lang haskell>{-# LANGUAGE TemplateHaskell #-} |
|||
import Factorial |
|||
⚫ | |||
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}}== |
=={{header|J}}== |