Haskell 学习笔记(二)
Haskell 类型
Haskell有一个静态类型系统。每个表达式的类型在编译时都是已知
Haskell 可以自己推断数据的类型
:t 可以来判断表达式的类型
Prelude> :t 'a'
'a' :: Char
Prelude> :t True
True :: Bool
Prelude> :t "hello"
"hello" :: [Char]
Prelude> :t 1==1
1==1 :: Bool
Prelude> :t (True,'a',1)
(True,'a',1) :: Num c => (Bool, Char, c)
类型声明
单参数例子
写一个函数 demo.hs 去掉所有的小写字母
removeNonUppercase :: [ Char ] - > [ Char ]
removeNonUppercase st = [c | c < - st, c`elem` [ 'A' .. 'Z' ]]
编译运行
Prelude> :l demo2
[1 of 1] Compiling Main ( demo2.hs, interpreted )
Ok, one module loaded.
*Main> rem
rem removeNonUppercase
*Main> removeNonUppercase ["Hello"]
<interactive>:2:21: error:
• Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the expression: "Hello"
In the first argument of ‘removeNonUppercase’, namely ‘["Hello"]’
In the expression: removeNonUppercase ["Hello"]
*Main> removeNonUppercase "Hello"
"H"
因为开始定义的为Char ->Char 所有[“Hello”]是不能在其中运行的, 必须传入的参数是"Hello" Char类型的。
多参数例子
三个整型相加
addThree :: Int -> Int -> Int -> Int
addThree x y z = x + y + z
编译运行
relude> :l demo3
[1 of 1] Compiling Main ( demo3.hs, interpreted )
Ok, one module loaded.
*Main> addThree 1 2 3
6
*Main> addThree [1,2,3]
<interactive>:3:10: error:
• Couldn't match expected type ‘Int’ with actual type ‘[Integer]’
• In the first argument of ‘addThree’, namely ‘[1, 2, 3]’
In the expression: addThree [1, 2, 3]
In an equation for ‘it’: it = addThree [1, 2, 3]
Int -> Int -> Int -> Int 参数之间用 ->来连接。 最后的一个 -> 指向返回类型。
Int 和 Integer
Int 为整型 有界限。 有最大和最小值 取决于机器。 比如在32位上, 最小就是–2147483648 最大值就是2147483647
常见的类型还有 Float Double Bool Char
类型变量type variable
前面用到的:t 也可以用来查看函数的类型
具有类型变量的函数称为多态函数
比如
Prelude> :t head
head :: [a] -> a
Prelude> :t tail
tail :: [a] -> [a]
Prelude> :t fst
fst :: (a, b) -> a
Prelude> fst (2,1)
2
head 函数取到的头元素 所以是 [a] -> a。而tail是取head之后的所有,还是一个list 所以是是[a] -> [a]
同理还有fst
再来看==也是作为一个函数
Prelude> :t (==)
(==) :: Eq a => a -> a -> Bool
Prelude> :t (/=)
(/=) :: Eq a => a -> a -> Bool
如果函数仅由特殊字符组成,则默认情况下它被视为中缀函数。如果我们想检查它的类型,将它传递给另一个函数或将其称为前缀函数,我们必须将它括在括号中。
同理还有 >,<,> =和<=
show 和 read
show 函数可以将将元素以字符串的形式输出
Prelude> :t show
show :: Show a => a -> String
Prelude> show "hello"
"\"hello\""
Prelude> show True
"True"
Prelude> show 1
"1"
read
Prelude> read "True" == False
False
Prelude> show 1
"1"
Prelude> show True
"True"
Prelude> read "[1,2,3]" ++ [3]
[1,2,3,3]
Prelude> read "4"
*** Exception: Prelude.read: no parse
Prelude> :t read
read :: Read a => String -> a
read “4” 不知道是什么类型所以无法使用,当我们给出适当类型的时候
Prelude> read "2" ::Int
2
Prelude> read "2" ::Float
2.0
Prelude> read "[1,2,3,4]" :: [Int]
[1,2,3,4]
bound
minBound maxBound
Prelude> minBound :: Int
-9223372036854775808
Prelude> maxBound :: Char
'\1114111'
Prelude> maxBound :: Bool
True
Prelude> :t maxBound
maxBound :: Bounded a => a
Num
数字类型
可以进行类型之间转换
Prelude> :t 20
20 :: Num p => p
Prelude> 20 :: Int
20
Prelude> 20 :: Double
20.0
Prelude> :t (*)
(*) :: Num a => a -> a -> a
Reference page
[1]: http://learnyouahaskell.com/types-and-typeclasses
上一篇: Haskell学习笔记:Input and Output
下一篇: 标准盒模型和 IE 盒模型