【发布时间】:2013-10-29 07:11:31
【问题描述】:
我经常对可选值使用列表推导:
[Parent parent, Destination [DestPage currPage]] ++ [OpenChildren | parent == Bookmark 0]
但我不知道如何选择而不是可选值。
我的很多代码是这样的:
let lblTabX = if isAtBottom then 5 else 3
lblTabY = if isAtBottom then 3 else 60
lblTabPosition = Position left (if isAtBottom then bottom else top)
lblTabWidth = if isAtBottom then lblPageX - 60 else 20
lblTabHeight = if isAtBottom then 20 else pageHeight - 80
lblTabMargin = if isAtBottom then Margin 0 3 else Margin 3 0
正如你看到的很多如果:)
所以我在玩一些运算符并想出了这个语法:
iif c l r = if c then l else r
infixl 8 <-/
(<-/) l c = iif c l
infixl 8 /->
(/->) = ($)
我喜欢前面的例子现在的样子:
let lblTabX = 5 <-/ isAtBottom /-> 3
lblTabY = 3 <-/ isAtBottom /-> 60
lblTabPosition = Position left (bottom <-/ isAtBottom /-> top)
lblTabWidth = (lblPageX - 60) <-/ isAtBottom /-> 20
lblTabHeight = 20 <-/ isAtBottom /-> (pageHeight - 80)
lblTabMargin = Margin 0 3 <-/ isAtBottom /-> Margin 3 0
这当然是一个玩具示例。我无意使用它。 但我只是好奇,除了 if 运算符之外,还有表达选择的语法吗?也许有列表推导?
【问题讨论】:
-
嗯,
iif isAtBottom 5 3看起来比5 <-/ isAtBottom /-> 3更简洁 -
顺便说一句,Haskell 的
if ... then ... else是 exact 等效于类 C 语言中的三元? :运算符(不是“the 三元运算符”,它恰好是这些语言中唯一存在的三元运算符。原则上,可能有很多其他三元运算符与 ifthenelse 决策无关)。 -
严格来说,到目前为止你得到的所有答案都在回答你的问题,但它们并没有给你一个好的答案。您根本不应该将所有这些条件混合在一起。将这些设置移动到某种配置对象中,为上下文构建适当的配置(例如,默认、atBottom、atTop、atLeft、atRight)并将该配置提供给您的表格绘图/操作函数。
-
你是如何在 Haskell 中开发 web 的?
标签: haskell list-comprehension ternary-operator