More Boolean and Comparison Expressions

前面已经看到许多布尔表达式和比较表达式了,这里做一个简单的汇总

布尔运算(Boolean operations)

对表达式
1.语法(syntax)

e1 andalso e2
e1 orelse e2
not e1
(* e1和e2都必须有类型:bool*)

2.计算(Evaluation)

  • e1 andalso e2
    e1e2结果同时为true,则其结果为true,其余情况为false
  • e1 orelse e2
    e1e2任一结果为true,则其结果为true,其余情况为false
  • not e1
    e1结果为true,则其结果为false
    e1结果为false,则其结果为true

注意:
在很多语言中的语法是这样:e1 && e2, e1 || e2, !e1(ML不存在&&和||,!是不同的东西)
andalso、orelse不是函数,not是函数(类型:fn: bool -> bool)。

编程风格

不使用布尔运算符的糟糕的风格,可读性非常糟糕,简洁明了才是更好的风格。

(* e1 andalso e2 *)    (* e1 orelse e2 *)    (* not e1 *)       
if e1                  if e1                 if e1
then e2                then true             then false
else false             else e2               else true

比较运算符(Comparisons)

操作符:= <> > < >= <=
> < >= <=可以用于int绑定,real绑定(浮点数),但是不能混合使用。

1 > 2    
3.0 > 2 (* 错误!需要转换类型Real.fromInt*) 
3.0 > Real.fromInt 2

= <>可以用于“相等类型”,int是“相等类型”,但不可用于real(后续讨论“相等类型”)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容