定义类型类
2015-07-07 10:17
274 查看
data Color = Red|Blue|Green class BasicEq a where isEqual :: a->a->Bool isEqual x y = not (isNotEqual x y) isNotEqual :: a->a->Bool isNotEqual x y = not (isEqual x y) instance BasicEq Bool where isEqual False False = True isEqual True True = True isEqual _ _ = False instance BasicEq Color where isEqual Red Red = True isEqual Blue Blue = True isEqual Green Green = True isEqual _ _ = True
定义类型类使用
class className xxx where
常用的函数show
其函数签名
*Main> :type show show :: Show a => a -> String
可知它是类型类Show里的一个方法,方法签名如上
同理看看我们的自定义
*Main> :type isEqual isEqual :: BasicEq a => a -> a -> Bool *Main>
使用时可以指定返回类型
*Main> (isEqual True False ):: Bool False
指定输入类型
*Main> isEqual Blue (Blue::Color) True
相关文章推荐
- Regular Expression Matching 递归
- 32、du命令
- Spring 使用注解方式进行事务管理
- 云服务器使用帮助
- SQL连接查询深度探险
- Could not connect to Redis at XX.XX.XX.XX:XX: Cannot assign requested address
- String c = new String("xyz")会创建几个String对象的问题
- SqlServer中查看当前Open的Cursor方法
- 社説 20150707 なでしこ準優勝 ブームから「文化」への契機に
- linux程序设计——取消一个线程(第十二章)
- pspice 仿真
- Java 运算符优先级
- LDAP问题汇总
- Matlab在曲线中如何标示极值点?
- mysql中通过not exists寻找两个表中的差异数据
- mysql的源码包方式安装(mysql5.5)
- chmod和chown的区别
- angular笔记
- Oracle中instr函数使用方法
- 日经社説 20150707 ギリシャはユーロ離脱への道を歩むのか