logo

Crowdly

Browser

Add to Chrome

Conditional computation For something less strange, let's see how we can take...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

Conditional computation

For something less strange, let's see how we can take decisions in Haskell. You're certainly familiar with if statements in your favorite programming language. But Haskell has no statements whatsoever.

You may also know that most programming languages offer a conditional expression construct. In C/Java/JavaScript, this construct is immediately recognizable by the unusual tokens ? and ::

<cond-expr> ? <then-expr> : <else-expr>

Python has the same construct, but with a different syntax that uses named keywords instead of punctuation:

<then-expr> if <cond-expr> else <else-expr>

Haskell's syntax is similar to Python in that it too uses named keywords, but in a more "linear" order, similar to what you expect from Java/JavaScript:

if <cond-expr> then <then-expr> else <else-expr>

For example, we could write:

ghci> if True then "yay" else "nay"

"yay"

Given that this is an expression, we can use it to compose larger expressions:

ghci> "result: " ++ if True then "yay" else "nay"

"result: yay"

(If you feel more comfortable adding explicit parentheses around the whole if expression, please do so in the future.)

To wrap up this introduction, let's combine what we have learned. Define a function named description with one boolean parameter, status, which produces a "result" string as shown above. It should work like this:

ghci> description True

"result: yay"

ghci> description False

"result: nay"

Write the definition of the function:

More questions like this

Want instant access to all verified answers on www.icorsi.ch?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome