Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator in haskell

I often use list comprehension for optional values:

[Parent parent, Destination [DestPage currPage]] ++ [OpenChildren | parent == Bookmark 0]

But i do not know how to do a choice instead of optional value.

A lot of my code looks like this:

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

As you see a lot of ifs :)

So i was playing with some operators and came up with this syntax:

iif c l r = if c then l else r

infixl 8 <-/
(<-/) l c = iif c l

infixl 8 /->
(/->) = ($)

And i like how the previous example now looks:

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

This is a toy example of course. I have no intention of using it. But i was just curious, is there a syntax to express the choice besides the if operator? Maybe with list comprehensions?

like image 207
Vagif Verdi Avatar asked Oct 29 '13 07:10

Vagif Verdi


People also ask

Does Haskell have ternary operator?

With a bit of work, we can define a ternary conditional operator in Haskell.

Which is a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Can we use ternary operator in ngClass?

'css-class-1' : 'css-class-2' . So ngClass still can use ternary operator in Angular 2.


1 Answers

ifs in Haskell aren't terribly pretty, but that's not really the reason they're used so seldom. It's more because there's usually a more elegant syntactic alternative! In your example, I would consider

let (                lblTabX, lblTabY, lblTabPosition, lblTabWidth, lblTabHeight,    lblTabMargin )
     | isAtBottom =( 5,       3,       bottom,         lblPageX-60, 20,              Margin 0 3   )
     | otherwise  =( 3,       60,      top,            20,          pageHeight - 80, Margin 3 0   )

Alternatively, you might define an already partially evaluated operator locally:

let bottomCase/|/topCase | isAtBottom = bottomCase
                         | otherwise  = topCase
    lblTabY         =                      3 /|/ 60
    lblTabPosition  = Position left $ bottom /|/ top
    lblTabWidth     =        (lblPageX - 60) /|/ 20
    lblTabHeight    =                     20 /|/ (pageHeight - 80)
    lblTabMargin    =             Margin 0 3 /|/ Margin 3 0

You definitely do not want to do the check on isAtBottom multiple times, redundant code is always bad, regardless of which syntax you use. But when you do just need a single decision based on a simple boolean value, I would stick to the standard if rather than defining custom operators.

like image 154
leftaroundabout Avatar answered Oct 21 '22 06:10

leftaroundabout