Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active September 26, 2021 17:08
Show Gist options
  • Save monadplus/6f14211d0690b2f8871fea88893cf56f to your computer and use it in GitHub Desktop.
Save monadplus/6f14211d0690b2f8871fea88893cf56f to your computer and use it in GitHub Desktop.
FizzBuzz
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE GADTs #-}
module FizzBuzz where
import Data.Singletons.TH
import Data.Singletons.Prelude
import Data.Singletons.TypeLits
$(singletons [d|
type Divisible n by = Mod n by == 0
type FizzBuzzElem :: Nat -> Symbol
type FizzBuzzElem n =
If
(Divisible n 15)
"FizzBuzz"
( If
(Divisible n 3)
"Fizz"
( If
(Divisible n 5)
"Buzz"
(Show_ n)))
|])
type FizzBuzz n = Fmap FizzBuzzElemSym0 (EnumFromTo 1 n)
-- |
-- >>> fizzBuzz @10
-- "[FizzBuzz,1,2,Fizz,4,Buzz,Fizz,7,8,Fizz]"
fizzBuzz :: forall (n :: Nat). (KnownSymbol (Show_ (FizzBuzz n))) => String
fizzBuzz = symbolVal (Proxy @(Show_ (FizzBuzz n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment