Haskell has unique type and typeclasses, in this post with exmaple, we will check on the following.
- :t command and the types
- typeclasses 101
- well-known type classes, Eq, Ord, Ordering, Num, Bounded
- show and read command
the example is as follow.
-- :t command to display the type of a operands :t 'a' :t "HELLO!" :t 4 == 5 :t (True, 'a') -- rewrite the removeNonUppercase with type declaration removeNonUppercase :: [Char] -> [Char] removeNonUppercase st = [c | c <- st, c `elem` ['A'..'Z']] -- type of a function that takes serveral parameters addThree :: Int -> Int -> Int -> Int addThree x y z = x + y + z -- do this on the ghci command line -- let addThree :: Int -> Int -> Int -> Int; addThree x y z = x + y + z -- Integer stands for really big integer, while Int has bound, 7 is Int, but 7.2 is not Int -- below show that you can define a factorial that takes in -- integer and returns integer, rather than Int returns Int factorial :: Integer -> Integer factorial n = product [1..n] factorial 50 -- type variables :t head -- it produce [a] -> a -- in haskell, type are in capitcal cases, so a is not a type, instead it is -- type variable -- -- much like generic types in other language. -- -- functions that has type variables are called polymorphic functions. :t fst -- Typeclasses 101 -- a typeclass is a sort of interface that defines some behavior, :t (==) -- produce -- (==) :: Eq a => a -> a -> Bool -- If a type is a part of a typeclass, that means that it supports and implements the behavior the typeclass describes. -- how is the before output read? -- the equality function takes any two values that are of the same type and returns a Bool. The type of those two values must be a member of the Eq class (this was the class constraint). -- read : http://learnyouahaskell.com/types-and-typeclasses -- for details -- import typeclasses -- Eq -- Ord :t (>) "Abrakadabra" < "Zebra" -- function compare takes two Ord and return an ordering -- ordering is a type that can be GT, LT or EQ.. "Abrakadabra" `compare` "Zebra" 5 >= 2 5 `compare` 3 -- Show typeclass and show function show 3 show 5.334 show True -- Read typeclass is opposite of Show, takes a string and returns a type which is a member of Read :t read -- it will produce -- read :: (Read a) => String -> a -- this will fail -- read "5" -- because ghci do not know what type you want read function to produce -- but you can use explicit -- type annotation read "5" :: Int read "5" :: Float read "[1,2,3,4]" :: [Int] read "(3, 'a')" :: (Int, Char) -- Enum -- members are sequentially ordered types . they can be enumerated. The main advantage of the Enum typeclass is that we can use its types in list ranges. ['a' .. 'e'] [LT .. GT] succ 'B' -- Bounded minBound :: Int maxBound :: Char maxBound :: Bool minBound :: Bool :t minBound -- produce -- minBound :: Bounded a => a -- Num -- Its members have the property of being able to act like numbers, let's examine the type of a number :t 20 -- it produce -- 20 :: Num a => a -- type -- Int -- Float -- Integer -- Double -- -- are all types that are in the Num typeclass :t (*) -- produces -- (*) :: Num a => a -> a -> a -- Integral -- include only integral (whole) numbers. -- Floating -- inlcude only floating numbers, so Float and Double -- author's opinion :t length -- length :: [a] -> Int -- the author believes that it should be -- (Num b) => length :: [a] -> b fromIntegral (length([1,2,3,4]) + 3.2
相关推荐
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的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...
Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...
"haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...
### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...
你可以在找到 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 编辑器中享受到强大的代码...