Created
May 17, 2012 06:06
-
-
Save MgaMPKAy/2716873 to your computer and use it in GitHub Desktop.
Calculate fib n in compile time with TemplateHaskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module Fib where | |
import Language.Haskell.TH | |
import Language.Haskell.TH.Syntax | |
fib' :: Int -> Int | |
fib' n | |
| n <= 2 = 1 | |
| otherwise = fib' (n - 1) + fib' (n - 2) | |
fib :: Int -> Q Exp | |
fib n = [| $(lift $ fib' n) |] | |
-- more : http://stackoverflow.com/questions/9243794/evaluating-a-function-at-compile-time-with-template-haskell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
import Fib | |
main = print $(fib 40){-# LANGUAGE TemplateHaskell #-} | |
import Fib | |
main = print $(fib 40) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment