Lesson 7 of 16

Guards

Guards

Guards are a clean alternative to nested if/then/else. They use | to list conditions:

bmi :: Double -> String
bmi b
  | b < 18.5  = "Underweight"
  | b < 25.0  = "Normal"
  | b < 30.0  = "Overweight"
  | otherwise = "Obese"

otherwise is just True — a catch-all guard.

Guards are evaluated top-to-bottom; the first matching branch wins.

Compared to if/then/else

Guards are preferred when you have three or more conditions:

-- Harder to read:
sign n = if n > 0 then "positive" else if n < 0 then "negative" else "zero"

-- Cleaner with guards:
sign :: Int -> String
sign n
  | n > 0     = "positive"
  | n < 0     = "negative"
  | otherwise = "zero"

Your Task

Define grade that maps a score to a letter grade:

  • 90–100 → "A"
  • 80–89 → "B"
  • 70–79 → "C"
  • otherwise → "F"

Then print grade 85.

Haskell loading...
Loading...
Click "Run" to execute your code.