if-statement - 如果在 Haskell 中有多重条件吗?
问题描述
我试图嵌套检查两个以上条件的守卫,如下所示:
f :: Int -> Int
f i
| bool1
| bool2 = a
| bool3 = a2
| otherwise = a3
| bool4
| bool5 = a4
| bool6 = a5
| otherwise = a6
| bool8
...
| otherwise = an
这给了我一个解析错误。这样做的正确方法是
用 && 展平警卫或
实现如下功能:
multiIf :: [Bool] -> [a] -> a
multiIf (x:xs) (l:ls) = if x then l else multiIf xs ls
multiIf _ _ = undefined
还是有其他方法?
解决方案
不是直接的,但MultiWayIf 扩展可以为您提供非常相似的风格。
{-# LANGUAGE MultiWayIf #-}
f :: Int -> Int
f i
| bool1
= if | bool2 -> a
| bool3 -> a2
| otherwise -> a3
| bool4
= if | bool5 -> a4
| bool6 -> a5
| otherwise -> a6
| bool8
...
| otherwise = an
推荐阅读
- python - 如何使用 Numpy 有效地创建条件列数组?
- javascript - 如何在nodejs中形成一个动态的json数组?
- postgresql - 是否可以保证 postgres 中并行聚合函数的最小工作人员数量?
- java - 如何将 YML 值放入 @Pattern(regexp = "HELLO|WORLD")
- vb.net - 如何使用 PostgreSQL 作为数据库和 vb.net windows 窗体在 DataGridView1 中发布数据
- python - 如何为具有扩展用户(onetoone 字段)关系的模型执行 put/patch 方法 django rest框架
- jquery - 从 ajax 调用传回的数据中选择特定项目
- sql - 简化 Oracle 中的现有查询
- c# - 在我给它一个输入之后,我的程序一直跳到 else 语句。我已经尝试了一些东西,但没有任何工作。(C#)
- android - 如何使用另一个外部xml视图android?