Multiple function definitions with template haskell

Suppose I have a data type like so:

data Color = Red | Blue | Green

How would I generate a function like this using templatehaskell?

myShow Red   = ...
myShow Blue  = ...
myShow Green = ...

ie I'm looking for multiple definitions for a function based on pattern-matching.


{-# LANGUAGE TemplateHaskell #-}

module Test where 

import Language.Haskell.TH

data Color = Red | Blue | Green

myShow' :: Q [Dec]
myShow' = return [FunD (mkName "myShow") [mkClause 'Red, mkClause 'Blue, mkClause 'Green]]
  where mkClause n = Clause [ConP n []] (NormalB $ LitE $ StringL $ nameBase n) []
链接地址: http://www.djcxy.com/p/20208.html

上一篇: 复制RDLC报告文本并粘贴到记事本中

下一篇: 使用模板haskell的多个函数定义