epicours/Algo/Séminaire/Chapter 3 - Case analysis.md

344 B

3.1. The alternative

The if structure

if cond then expr1 else expr2

⚠️ expr1 and expr2 have to be the same type. cond is a bool

For exemple

# if 1<2 then "higher" else "lower" ;;
-: string = "higher"

Exercise : absolute program

# let abs(x) = 
	if x>0 then x
	else -x ;;;