haskell - F#有多路if吗?

标签 haskell if-statement f# pattern-matching

有没有办法重构这个:

let collide (b1 : Box) (b2 : Box) =
  if   bottom b1 > top b2
  then false
  else if   top b1 < bottom b2
       then false
       else if   right b1 < left b2
            then false
            else if   left b1 > right b2
                 then false
                 else true

以比这更易读的方式:

let collide (b1 : Box) (b2 : Box) =
  match () with
  | _ when bottom b1 > top    b2 -> false
  | _ when top    b1 < bottom b2 -> false
  | _ when right  b1 < left   b2 -> false
  | _ when left   b1 > right  b2 -> false
  | _                            -> true

我正在考虑类似于 GHC 7.6.1 中的多路 if 表达式的东西:http://www.haskell.org/ghc/docs/7.6.1/html/users_guide/syntax-extns.html#multi-way-if .

最佳答案

let collide (b1 : Box) (b2 : Box) = 
    if   bottom b1 > top b2 then false 
    elif top b1 < bottom b2 then false 
    elif right b1 < left b2 then false 
    elif left b1 > right b2 then false 
    else true 

关于haskell - F#有多路if吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593993/

相关文章:

haskell 初始累加器 'null' 值

multithreading - 了解线程化 GHC haskell 程序的子进程

list - Haskell:计算列表中每对相邻元素的差异

php - IF 规则 $_POST PHP - MYSQL

java - 使用二进制搜索在正确的索引处打开数组中的空间

.net - 异步等待 n 件事发生

.net - 执行 F# 脚本

haskell - do block 内输入 '<-' 的解析错误?

c - 如何仅当变量更改为特定值时使 if 条件为 true

f# - 为什么结构值必须是可变的才能设置索引属性?