本博客所有内容是原创,未经书面许可,严禁任何形式的转载。
http://blog.csdn.net/u010255642
我们继续看几个类型类,猜测一下用途
Prelude> :t (>=)
(>=) :: Ord a => a -> a -> Bool
这个用途比较明显,比较大小,a -> a 说明比较的类型得一致,最后的Bool表示然后返回一个Bool型,Ord是类型类,对a进行类型约束,ord是比较类
Prelude> :t compare
compare :: Ord a => a -> a -> Ordering
Prelude>
Prelude> compare "abc" "dbb"
LT
Prelude> compare 123 456
LT
Prelude> compare 8123 456
GT
Prelude> compare 8123 8123
EQ
Compare比较后,返回一个较直观的对比结果
Prelude> :t (show)
(show) :: Show a => a -> String
Prelude> :t (read)
(read) :: Read a => String -> a
Show将参数转换为String类型,而Read将String转化为参数本身应有的类型
Prelude> show True
"True"
Prelude> show 12.34
"12.34"
Prelude> show [12,33]
"[12,33]"
Read函数在无法确定a代表的类型时,无法执行。
Prelude> read "124"
<interactive>:48:1:
Ambiguous type variable `a0' in the constraint:
(Read a0) arising from a use of `read'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: read "124"
In an equation for `it': it = read "124"
我们可以使用“::”进行类型注释
Prelude> read "124"::Int
124
Prelude> read "124"::Float
124.0
Prelude> read "[12,33,66]"::[Int]
[12,33,66]
或给read一个 暗示
Prelude> read "124"+232
356