We have already covered some existing haskell types and typeclasses. In this chapter, we'll learn how to make our own and how to put them to work!
First we will introduce new types and we will see how we can create new types with the "data" keyword.
- Declare a new type with "data" keyword and value constructor
- the record syntax
and as a sub-module, we will discuss the topic such as
- export a value constructor
- e.g. of record syntax
FIst let's see an example which shows basics of defining types such as Shape and points.
-- fie: -- own_type.hs -- description: -- create your own types and type classes -- Algebraic data types intro -- -- data: -- e.g. how the Bool type is defined. data Bool = False | True data Int = -2147483648 | -2147483647 | ... | -1 | 0 | 1 | 2 | ... | 2147483647 -- e.g. suppose that we are defining shapes which could be a Circle or a Rectangle -- data Shape = Circle Float Float Float | Rectangle Float Float Float Float -- -- value constructors: -- Cirlce as well as Rectangle are like value contructors -- Circle: Value contructors which takes 3 parameters -- Rectangle: Value constructors which takes 4 parameters :t Circle :t Rectangle -- make a function takes a Shape and returns something surface :: Shape -> Float surface (Circle _ _ r) = pi * r ^ 2 surface (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1) -- surface $ Circle 10 20 10 -- surface $ Rectangle 0 0 100 100 -- -- make Shape showable: data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving (Show) -- haskell automatically make "Shape" part of the Show typeeclass Circle 10 20 5 Rectangle 50 230 60 90 -- more usage on constructor as functions map (Circle 10 20) [4, 5, 6, 6] -- make it better -- re-org the type classes data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show) surface :: Shape -> Float surface (Circle _ r) = pi * r ^ 2 surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1) surface (Rectangle (Point 0 0) (Point 100 100)) surface (Circle (Point 0 0) 24) -- yet anoter example nudge :: Shape -> Float -> Float -> Shape nudge (Circle (Point x y) r) a b = Circle (Point (x + a) (y + b)) r nudge (Rectangle (Point x1 y1) (Point x2 y2)) a b = Rectangle (Point (x1+a) (y1+b)) (Point (x2+a) (y2+b)) -- auxiliary functions to help dealing with Points baseCircle :: Float -> Shape baseCircle r = Circle (Point 0 0) r baseRect :: Float -> Float -> Shape baseRect with height = Rectangle (Point 0 0) (Point width height) -- nudge (baseRect 40 100) 60 23 -- -- -- exports both the type and functions within -- -- client use our code will by deafult import Rectangle, Circle and others without doing -- Shape (Rectangle, Circle) module Shapes ( Point(..) -- all the value constructors for Shape , Shape(..) , surface , nudge , baseCircle , baseRect ) where
But as we see that the Person type defined above is less readable, can we make it more readable?Here comes the record syntax, where it gives a understandable field names,. before we goes into the record, sytnax, let re-examine the person class.
-- file: -- Person.hs -- description: -- the file defines the Person object -- -- Record Syntax module Person ( Person(..) , firstName , lastName , age , height , phoneNumber , flavor ) where data Person = Person String String Int Float String String deriving (Show) firstName :: Person -> String firstName (Person firstName _ _ _ _ _) = firstName lastName :: Person -> String lastName (Person _ lastName _ _ _ _) = lastName age :: Person -> Int age (Person _ _ age _ _ _ ) = age height :: Person -> Float height (Person _ _ _ height _ _) = height phoneNumber :: Person -> String phoneNumber (Person _ _ _ _ number _) = number flavor :: Person -> String flavor (Person _ _ _ _ _ flavor) = flavor -- -- alternative way to write data types -- with mnemonic such as follo -- NOTE : -- Check on the Person2.hs
and then let's examine the new record sytnax, where you can define person class as follow. Together there are other types such as Car and methods which operate on Cars and Person.
-- file: -- Person2.hs -- description: -- Person2 module Person (Person(..) ,Car(..) ) where data Person = Person { firstName :: String , lastName :: String , age :: Int , height :: Float , phoneNumber :: String , flavor :: String } deriving (Show) -- Check the type of flavor and firstName -- :t flavor -- :t firstName -- data Car String String String Int deriving (Show) data Car = Car { company :: String, model :: String, year :: Int } deriving (Show) -- Car { company = "Ford", model = "Mustang", year=1967} -- Create the Car object -- Car "Ford" "Mustang" 1967 -- -- -- Type Parameter : -- the type parameter a data Maybe a = Nothing | Just a data Car = Car { company :: String , model :: String , year :: Int } deriving (Show) -- with type paramter data Car a b c = Car { company :: a , model :: b , year :: c } deriving (Show) tellCar :: Car -> String tellCar (Car {company = c, model = m, year = y}) = "This " ++ c ++ " " ++ m ++ " was made in " ++ show y -- to use the tellCar -- ghci> let stang = Car {company="Ford", model="Mustang", year=1967} -- ghci> tellCar stang -- "This Ford Mustang was made in 1967" tellCar :: (Show a) => Car String String a -> String tellCar (Car {company = c, model = m, year = y}) = "This " ++ c ++ " " ++ m ++ " was made in " ++ show y -- how to use the new tellCar method which has the new type paramter. -- ghci> tellCar (Car "Ford" "Mustang" 1967) -- "This Ford Mustang was made in 1967" -- ghci> tellCar (Car "Ford" "Mustang" "nineteen sixty seven") -- "This Ford Mustang was made in \"nineteen sixty seven\"" -- ghci> :t Car "Ford" "Mustang" 1967 -- Car "Ford" "Mustang" 1967 :: (Num t) => Car [Char] [Char] t -- ghci> :t Car "Ford" "Mustang" "nineteen sixty seven" -- Car "Ford" "Mustang" "nineteen sixty seven" :: Car [Char] [Char] [Char]
相关推荐
Atom-haskell-ghc-mod.zip,haskell-ghc-mod atom packagehaskell ghc mod atom包,atom是一个用web技术构建的开源文本编辑器。
haskell-mode emacs haskell-mode emacs
Atom-ide-haskell-hoogle.zip,在光标下显示符号的滚动信息艾德·哈斯克尔·胡格尔,atom是一个用web技术构建的开源文本编辑器。
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
Programming-in-Haskell-2nd-Edition.pdf
从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-英文版
用于 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 ...
演示医疗用例的参考DAML应用程序 -Haskell-TypeScript-下载
haskell-chart, haskell的2D 图表库 图 haskell的2D 图表库进一步的信息可以在关联的 wiki中找到。
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
A History of Haskell - Being Lazy With Class
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 ...
haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...
Atom-atom-haskell-scry.zip,De-emphasize qualified Haskell identifiers.SCRY,atom是一个用web技术构建的开源文本编辑器。
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid 已加书签
安装过程中应在路径上放置一个名为haskell-in-haskell的可执行文件。 否则,您可以使用cabal run haskell-in-haskell --直接运行项目,然后输入要传递的参数。 编译中 要编译Haskell文件,只需运行: haskell-in-...