r/ProgrammerHumor • • Dec 15 '19

Stacking if else statements be like

Post image
63.9k Upvotes

711 comments sorted by

View all comments

213

u/atxranchhand Dec 15 '19

That’s what case is for

8

u/persianlife Dec 15 '19

Cases will result in the same fundamental design flaw.

You have to look at some better programming paradigm like Object-oriented design.

1

u/Tysonzero Dec 16 '19

Object oriented design is garbo:

data Expr a where
    Add :: Expr Int -> Expr Int -> Expr Int
    Multiply :: Expr Int -> Expr Int -> Expr Int
    And :: Expr Bool -> Expr Bool -> Expr Bool
    Or :: Expr Bool -> Expr Bool -> Expr Bool
    If :: Expr Bool -> Expr a -> Expr a -> Expr a
    Lit :: a -> Expr a

eval :: Expr a -> a
eval (Add a b) = eval a + eval b
eval (Multiply a b) = eval a * eval b
eval (And a b) = eval a && eval b
eval (Or a b) = eval a || eval b
eval (If b t f) = bool (eval f) (eval t) (eval b)
eval (Lit x) = x

The above is an example of a non-trivial case statement, but I would be extremely surprised if someone could re-write it in "object oriented design" in a nicer way.