Now that you have made your own type and typeclasses. how to make these two associated. As you might have known that a type can be made an instance of a typeclasss if it supports that behavior.
e.g. An Int type type is an instance of the Eq typeclass because the Eqtypeclass defines behavior for stuff that can be equated. And because integers can be equated, Int is a part of the Eq typeclass. The real usefulness comes with the functions that act as the interface for Eq, namely == and /=. If a type is a part of the Eq typeclass, we can use the == functions with values of that type. That's why expressions like 4 == 4 and "foo" /= "bar" typecheck.
Haskell can automatically make our type an instance of any of the following typeclasses: Eq, Ord, Enum, Bounded, Show, Read.
we will see from this post the following.
- with the deriving keyword
- with the instances keyword
- instance constraint- such as a type is a sub-type of another.
Let's see the Person type that derives from the type such as Show
-- file -- derived_instances.hs -- description: -- a typeclaess is a sort of interface that defines some behaviors -- a type can be made of an instance of a typeclass if it supports the behavior -- for stuff tha can be equated -- e.g. the Eq typeclass, where Int is a part of the Eq typeclass, the real usefulness of the Eq typeclass we can use -- == function with values of that types. -- this is why the expression like 4 == 4 and "foo" /= "bar" typecheck. -- a person class that has implemented the person class -- data Person = Person { firstName :: String -- , lastName :: String -- , age :: Int -- } deriving (Eq) -- ghci> let mikeD = Person {firstName = "Michael", lastName = "Diamond", age = 43} -- ghci> let adRock = Person {firstName = "Adam", lastName = "Horovitz", age = 41} -- ghci> let mca = Person {firstName = "Adam", lastName = "Yauch", age = 44} -- ghci> mca == adRock -- False -- ghci> mikeD == adRock -- False -- ghci> mikeD == mikeD -- True -- ghci> mikeD == Person {firstName = "Michael", lastName = "Diamond", age = 43} -- True -- now, let make the Person class part of the typeclass of -- String, String, Int data Person = Person { firstName :: String , lastName :: String , age :: Int } deriving (Eq, Show, Read) -- ghci> let mikeD = Person {firstName = "Michael", lastName = "Diamond", age = 43} -- ghci> mikeD -- Person {firstName = "Michael", lastName = "Diamond", age = 43} -- ghci> "mikeD is: " ++ show mikeD -- "mikeD is: Person {firstName = \"Michael\", lastName = \"Diamond\", age = 43}" -- ghci> read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person -- Person {firstName = "Michael", lastName = "Diamond", age = 43} -- We can also read parameterized types, but we have to fill in the type parameters. So we can't do read "Just 't'" :: Maybe a, but we can do read "Just 't'" :: Maybe Char. -- you can even compare Nothing with Just a -- because Nothing comes before Just so Nothing is smaller than anything else. -- -- ghci> Nothing < Just 100 -- True -- ghci> Nothing > Just (-49999) -- False -- ghci> Just 3 `compare` Just 2 -- GT -- ghci> Just 100 > Just 50 -- True -- we cannot do this -- Just(*3) > Just (*2) -- because functions are not comparable. -- data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday -- you can make it a part of the Enum typeclass data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum) -- minBound :: Day maxBound :: Day succ Monday pred Saturday --
As we have seen that in this example, when we derive the person classes from the Bounded and Enum class, so we have gain the ability to apply minBound and Succ/Pred on the Day instances?
Though we might have a dedicate section which we will discuss the how to make instance type to an typeclasses, but let's me to give you a preview on what it looks like ..
data TrafficLight = Red | Yellow | Green instance Eq TrafficLight where Red == Red = True Green == Green = True Yellow == Yellow = True _ == _ = False instance Show TrafficLight where show Red = "Red Light" show Yellow = "Yellow light" show Green = "Green light" -- to specify some sort of constraint, such as below -- where to compare maybe types, you need to first be -- able to compare what the maybe contains. instance (Eq m) => Eq (Maybe m) where Just x == Just y = x == y Nothing == Nothing = True _ == _ = False
We will introduce this more in details on the typeclasses 102.
相关推荐
通过了解记录语法(Record Syntax)、类型参数(Type parameters)、派生实例(Derived instances)和类型别名(Type synonyms),读者可以创建更加丰富的数据模型。 第九章涉及输入与输出(Input and Output),Haskell通过...
- **Derived Instances**:了解如何为自定义类型自动推导出类型类实例。 - **Type Synonyms**:介绍类型别名的定义和用途。 - **递回地定义数据结构**:讲解如何使用递归来定义复杂的自定义数据结构。 - **...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。