Loading...
intermediate
Problem 06 • Step 02
abs
Absolute value: if a number is negative, make it positive. Otherwise, leave it alone.
This is your first time using if from the programmer’s side. Until now you implemented the if rule in the evaluator. Now you’re a user of it.
There are two cases:
- When
nis negative, you want to negate it - When
nis zero or positive, you want to return it unchanged
The conditional part is straightforward — you’ve already seen ["if", condition, then, else].
The tricky part is negation. The language has no unary minus operator. You can’t write “negative n.” But you do have subtraction — what two numbers can you subtract to get the negation of n?