haskell(10)

本博客所有内容是原创,未经书面许可,严禁任何形式的转载。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值