Anonymous user
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
<lang haskell>
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
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}}==
|