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.
相关推荐
Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...
在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...
在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...
**哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...
Get Programming with HASKELL-2018-英文版
【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...
用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...
Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...
### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...
在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...
Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...
### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...
"haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...
Atom-IDE-Haskell-HIE 是一个专门为 Haskell 开发者设计的 Atom 文本编辑器插件,它集成了 Haskell IDE Engine (HIE) 的 Language Server Protocol (LSP) 功能。这个插件允许开发者在 Atom 编辑器中享受到强大的代码...