A value constructor can take some values parameters and then produce a new value. For instance, the Car constructor takes three values and produces a car value. In a similar manner, type constructors can take types as parameters to produce new types. This might sound a bit too meta at first, but it's not that complicated. If you're familiar with templates in C++, you'll see some parallels. To get a clear picture of what type parameters work like in action, let's take a look at how a type we've already met is implemented.
an example of this is as below.
data Maybe a = Nothing | Just a
as the example shows here, a is a t ype paramter, because there is a type paramter involved, we call maybe a type constructor, Depending on what we want this data type to hold when it is not nothing. We may end up with Maybe Int, Maybe Car..
- type parameter
- type constraint
Let's first see some type without type parameter
-- file -- Car2.hs -- description: -- a car with a more variant things than others module Car (Car(..) ) where -- the car record type does not use the Record syntax data Car = Car { company :: String , model :: String , year :: Int } deriving (Show) tellCar :: Car -> String tellCar (Car {company = c, model = m, year = y}) = "This " ++ c ++ " " ++ m ++ " was made in " ++ show y -- use the following to test the tellCar funciton -- tellCar ( Car {company="Ford", model="Mustang", year=1967} )
then we compare that with the type parameter what we can do ..
, model :: b , year :: c } deriving (Show) tellCar :: (Show a) => Car String String a -> String -- does this mean that you have created an alias to company with c,and alias to model with m and alias to year with y tellCar (Car {company = c, model = m, year = y}) = "This " ++ c ++ " " ++ m ++ " was made in " ++ show y
the type parameter has parameterized type, such as the following. Data.Map type, which has a
Map k v
but there is some requirement on the key type, it has to be an Ord typeclass instance, so that we can do comparison on the key and we can sort keys with the same hash value.
so, her e is what you can do to specify the type constraint.
data (Ord k) => Map k v = ...
But normally we don't put the constraint on the data declaration, if we don't put the constraint on the data declaration, we are going to have to put the constraint into functions that assmes the keys in a map can be ordered. But if we don't put the constraint in the data declaration, we don't have to put (Ord k) => in the type declaration of functions that don't care whether the jeys can be odered. e.g. of this is the unction is toList, that just takes a mapping and converts it to an associative list. Its type signature istoList :: Map k a -> [(k, a)]. If Map k v had a type constraint in its data declaration, the type for toList would have to be toList :: (Ord k) => Map k a -> [(k, a)], even though the function doesn't do any comparing of keys by order.
Let's see yet another example , this time, we will make a Vector type,
-- file -- type_paramter.hs -- description: -- this is a type paramter illustration on the map types module Vector ( Vector(..) ) where -- If we were defining a mapping type, we could add a typeclass constraint in the data declaration: -- data (Ord k) => Map K v = ... -- NOTE: it's a very strong convention in Haskell to never add typeclass constraints in data declarations. Why? data Vector a = Vector a a a deriving (Show) -- vector plus vplus :: (Num t) => Vector t -> Vector t -> Vector t (Vector i j k) `vplus` (Vector l m n) = Vector (i+l) (j+m) (k+n) -- vector Mult vectMult :: (Num t) => Vector t -> t -> Vector t (Vector i j k) `vectMult` m = Vector (i*m) (j*m) (k*m) -- vector scalarMult scalarMult :: (Num t) => Vector t -> Vector t -> t (Vector i j k) `scalarMult` (Vector l m n) = i*l + j*m + k*n -- NOTE on the vector classes -- note that the vector class can only operate on the same Vector class and can work on Float, Int, and Double. -- -- ghci> Vector 3 5 8 `vplus` Vector 9 2 8 -- Vector 12 7 16 -- ghci> Vector 3 5 8 `vplus` Vector 9 2 8 `vplus` Vector 0 2 3 -- Vector 12 9 19 -- ghci> Vector 3 9 7 `vectMult` 10 -- Vector 30 90 70 -- ghci> Vector 4 9 5 `scalarMult` Vector 9.0 2.0 4.0 -- 74.0 -- ghci> Vector 2 9 3 `vectMult` (Vector 4 9 5 `scalarMult` Vector 9 2 4) -- Vector 148 666 222
Once again, it's very important to distinguish between the type constructor and the value constructor. When declaring a data type, the part before the = is the type constructor and the constructors after it (possibly separated by |'s) are value constructors. Giving a function a type of Vector t t t -> Vector t t t -> t would be wrong, because we have to put types in type declaration and the vector type constructor takes only one parameter, whereas the value constructor takes three. Let's play around with our vectors.
相关推荐
Font Awesome图标字体库提供可缩放矢量图标,它可以被定制大小、颜色、阴影以及任何可以用CSS的样式
介绍了physical design的floorplanning问题
数学建模培训资料 数学建模实战题目真题答案解析解题过程&论文报告 最低生活保障问题的探索 共20页.pdf
变更用水性质定额申请表.xls
从官网上下载下来,作为资源存储,方便安装,此资源为windows版本
嗨玩旅游网站-JAVA-基于springboot嗨玩旅游网站设计与实现(毕业论文+PPT)
【资源说明】 本科毕业设计 基于Python中国知网(cnki)爬虫及数据可视化详细文档+全部资料.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
api代码
【作品名称】:基于 Java 实现的24点卡牌游戏【课程设计】 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: Java小游戏--24点卡牌游戏 将扑克牌(除大小王)随机打乱,每次出现4张卡牌,每张卡牌使用一次,13个回合。 A代表1,J代表11,Q代表12,K代表13。 可2-4人局域网同时在线对战,100秒倒计时结束前回答正确可获得积分,先回答的可获4分,后回答的分数依次递减。 实时显示玩家排名。 【资源声明】:本资源作为“参考资料”而不是“定制需求”,代码只能作为参考,不能完全复制照搬。需要有一定的基础看懂代码,自行调试代码并解决报错,能自行添加功能修改代码。
用 Python 实现的可扩展布隆过滤器皮布卢姆pybloom是一个包含 Bloom Filter 数据结构以及可扩展 Bloom Filter 实现的模块,如下所述P. Almeida、C.Baquero、N. Preguiça、D. Hutchison,可扩展布隆过滤器,(GLOBECOM 2007),IEEE,2007。如果您了解需要提前留出多少位来存储整个集合,那么布隆过滤器就是您的不二之选。可扩展布隆过滤器允许您的布隆过滤器位根据误报概率和大小进行增长。当过滤器达到容量上限时,即为“满”M * ((ln 2 ^ 2) / abs(ln p)),其中 M 是位数,p 是误报概率。当达到容量上限时,将创建一个比上一个过滤器大得多的新过滤器,其误报概率更小,哈希函数数量更多。>>> from pybloom import BloomFilter>>> f = BloomFilter(capacity=1000, error_rate=0.001)>>> [f.add(x) for x in range(10)][False, False, False,
计算机学院宿舍美化大赛.rar
基于java的运动器械购物商城设计与实现.docx
内容概要:文章介绍了针对“卓越工程师教育培养计划”,结合PBL和CDIO工程教育理念,对材料成型及控制工程专业课程设计的实践教学改革进行探索。首先在命题设计上依托企业实践项目,确保设计内容与生产实际紧密结合,具有较强的创新性和实用性。在过程管理中,采用分组合作和面向实际问题导向的教学方法,提升学生的工程素养和创新思维。通过课程设计的成绩考核,结合校内外导师的共同评价,客观全面衡量学生的学习成果。指导教师发挥了组织、支持和引导等多方面的角色作用。 适合人群:高等院校材料成型及控制工程专业学生和教学管理人员;工程教育领域的研究人员。 使用场景及目标:旨在提升工科学生的工程实践能力和创新能力,使其具备解决复杂实际工程问题的能力。通过改革教学内容和方法,改善传统课程设计中存在的不足,培养出高素质的技术人才。 其他说明:改革措施在实际运行中取得了较好的教学效果,提高了学生的就业竞争力,但仍存在一些不足之处需要在未来进行完善。
设计模式学习
C的两数相加求和的程序代码
Viper是一个基于Anno微服务引擎开发的Dashboard示例项目。Anno底层通讯采用grpc、thrift
本教程播放列表涵盖了 Python 中的数据结构和算法。每个教程都有数据结构或算法背后的理论、BIG O 复杂性分析和可供练习的练习。使用 Python 的数据结构和算法本教程涵盖了 Python 中的数据结构和算法。每个教程都包含数据结构或算法背后的理论、BIG O 复杂度分析以及可供练习的练习。要观看视频,您可以访问播放列表https://www.youtube.com/playlist?list=PLeo1K3hjS3uu_n_a__MI_KktGTLYopZ12订阅 codebasics youtube 频道https://www.youtube.com/c/codebasics
python入门——安装Python软件包
就业去向信息-JAVA-基于微信小程序高校毕业生实习及就业去向信息管理系统(毕业论文+PPT)
基于java的学费管理系统设计与实现.docx