Created
September 6, 2013 14:58
-
-
Save pasberth/6465004 to your computer and use it in GitHub Desktop.
Template Haskell でコンパイル時 FizzBuzz
This file contains 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 FizzBuzz where | |
import Language.Haskell.TH | |
fizzbuzz :: Int -> ExpQ | |
fizzbuzz 0 = [| putStrLn "FizzBuzz" |] | |
fizzbuzz n = appE (appE (varE (mkName ">>")) | |
(fizzbuzz (n - 1))) | |
x where | |
x :: ExpQ | |
x | |
| n `mod` 15 == 0 = [| putStrLn "FizzBuzz" |] | |
| n `mod` 3 == 0 = [| putStrLn "Fizz" |] | |
| n `mod` 5 == 0 = [| putStrLn "Buzz" |] | |
| otherwise = [| print n |] |
This file contains 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 Main where | |
import FizzBuzz | |
main = $(fizzbuzz 15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment