【发布时间】:2015-09-07 22:53:47
【问题描述】:
有没有更简洁的方式来编写下面的haskell代码:
{-# LANGUAGE FlexibleInstances #-}
class Greetable g where
hi :: g -> String
-- Here i should list all instances of Num
instance Greetable Float where
hi s = "Hi number! " ++ show s
instance Greetable Int where
hi s = "Hi number! " ++ show s
instance Greetable Double where
hi s = "Hi number! " ++ show s
-- Etc.
-- Here is other stuff
instance Greetable String where
hi s = "Hi string! " ++ s
编辑:我希望 hi 函数能够解释 Num 类的任何未来实例。
【问题讨论】: