Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created September 6, 2013 14:58
Show Gist options
  • Save pasberth/6465004 to your computer and use it in GitHub Desktop.
Save pasberth/6465004 to your computer and use it in GitHub Desktop.
Template Haskell でコンパイル時 FizzBuzz
{-# 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 |]
{-# 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